【问题标题】:Solidity 0.5.0: TypeError Initial value for constant variable has to be compile-time constantSolidity 0.5.0:TypeError 常量变量的初始值必须是编译时常量
【发布时间】:2019-06-03 17:25:46
【问题描述】:

为什么我不能在 Solidity 0.5.0 中以这种方式声明常量?使用最新版本一切正常:

uint256 public constant INITIAL_SUPPLY = 10000 * (10 ** uint256(decimals()));

/**
 * @return the number of decimals of the token.
 */
function decimals() public view returns (uint8) {
    return _decimals;
}

【问题讨论】:

    标签: ethereum solidity


    【解决方案1】:

    在 Solidity 中,常量不会存储在任何地方;它们在字节码中被替换。大致是这样的:

    constant uint256 FOO = 42;
    
    function blah() {
        return FOO;
    }
    

    变成这样:

    function blah() {
        return 42;
    }
    

    只有当常量的值在编译时已知时,编译器才能进行这种替换。在您的示例中,如果 _decimals 是一个常量,则编译器理论上可以确定 decimals() 返回一个常量以及该值是什么,但 Solidity 编译器远没有那么聪明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      • 2021-04-22
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      相关资源
      最近更新 更多