[Contract]Solidityのusing xx for xxを一回分かります.
2709 ワード
using A for*; # Aの関数を任意の種類に付けて使用します.
using A for Bxiとは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);
}
}
リンク:https://www.cnblogs.com/farwish/p/12560568.html