E4142#
此 'const' 声明不是常量。
在 MoonBit 中,您可以使用 const 声明常量值。只有不可变原语类型的字面量才能分配给 const。
这些是常量值:
1、"String"、1.0、true、false等。这些不是常量值:
[1, 2, 3]、1 + 1、fn() { 1 }等。
错误示例#
pub const A : Int = [1, 2, 3].length() // Error: This 'const' declaration is not constant.
建议#
如果无法在编译时运行计算并将结果分配给 const,您可以使用 let 声明在初始化时间计算这些结果。
pub let a : Int = [1, 2, 3].length() // This will be computed at initialization time.
如果您可以使用计算器自行计算值,您只需将结果分配给 const 即可。
pub const A : Int = 3 // This is a constant value.