http://www.7klian.com

Solidity 中 immutable (不行变量)与constant(常量)

immutable 不行变量
}
[3] Tiny熊: https://learnblockchain.cn/people/15
immutable 修饰的变量是在陈设的时候确定变量的值, 它在结构函数中赋值一次之后,就不在改变, 这是一个运行时赋值, 就可以清除之前 constant 不支持利用运行时状态赋值的限制.
immutable不行变量同样不会占用状态变量存储空间, 在陈设时,变量的值会被追加的运行时字节码中, 因此它比利用状态变量自制的多, 同样带来了更多的安详性(确保了这个值无法在修改).
contract Example {
[4] 知乎: https://www.zhihu.com/people/xiong-li-bing
[2] ERC20: https://learnblockchain.cn/2018/01/12/create_token
    address immutable owner = msg.sender;
    }
    string constant TEXT = “abc”;
       decimals = _decimals;
References
[5] 微博: http://weibo.com/xionglb
    uint public constant decimals_constant;
原文链接: https://learnblockchain.cn/article/1059 
       maxBalance = _reference.balance; 
[6] GitHub: https://github.com/xilibi2003
constant 修饰的变量需要在编译期确定值, 链上不会为这个变量分派存储空间, 它会在编译时用详细的值替代, 因此, constant常量是不支持利用运行时状态赋值的(譬喻: block.number , now , msg.sender 等 )
contract C {
[7] 登链社区: https://learnblockchain.cn/

    function Example(uint _decimals, address _reference) public {
以下是常量的声明:
这个特性在许多时候很是有用, 最常见的如 ERC20[2]代币用来指示小数位置的decimals 变量, 它应该是一个不能修改的变量, 许多时候我们需要在建设合约的时候指定它的值, 这时 immutable 就大有用武之地, 雷同的尚有要生存 建设者地点, 关连系约地点.
}
constant 常量
    uint constant X = 32**22 + 8;
    uint immutable maxBalance;
[1] Solidity 0.6.5: https://learnblockchain.cn/docs/solidity/

Solidity 0.6.5[1] 更新引入了一个新的要害字 immutable , 它与之前的constant常量有何差异呢?
pragma solidity >0.6.4 <0.7.0;
constant 今朝仅支持修饰 strings 及 值范例.
       decimals_constant = _decimals; // TypeError: Cannot assign to a constant variable.
以下是 immutable 的声明举例:
        return _other.balance > maxBalance;
    function isBalanceTooHigh(address _other) public view returns (bool) {
    uint immutable decimals;
    }

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。