[Contract] 一次搞懂 Solidity 的 using xx for xx

通过llama.cpp与羊驼聊天的网页界面- 详解 Serge 的启动使用

 

using A for *;     # 把 A 的函数附给任意类型使用

using A for B;  # 意思是把 A 中的方法附给 B 使用

使用上面的方式,那么在我们的合约中定义了 B 类型的变量后,就可以像 B.xx() 这样使用 A 库的函数了。

 

举个局部例子:_miners 可以直接使用 has 方法。

library Roles {
    struct Role {
        mapping (address => bool) bearer;
    }

    /**
     * @dev Check if an account has this role.
     * @return bool
     */
    function has(Role storage role, address account) internal view returns (bool) {
        require(account != address(0), "Roles: account is the zero address");
        return role.bearer[account];
    }
}

contract MinterRole is Context {
    using Roles for Roles.Role;

    event MinterAdded(address indexed account);
    event MinterRemoved(address indexed account);

    Roles.Role private _minters;

    constructor () internal {
        _addMinter(_msgSender());
    }

    modifier onlyMinter() {
        require(isMinter(_msgSender()), "MinterRole: caller does not have the Minter role");
        _;
    }

    function isMinter(address account) public view returns (bool) {
        return _minters.has(account);
    }
}

 

Link:https://www.cnblogs.com/farwish/p/12560568.html

posted on 2020-03-24 18:35  ercom  阅读(561)  评论(0编辑  收藏  举报