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