E4036#
无法创建只读类型的值。
错误示例:#
在 lib/lib.mbt
中,我们声明了一个带有私有字段 __private
的结构体 R
:
pub(all) struct R {
x : Int
priv __private : Int
}
pub fn R::new(x: Int) -> R {
{x, __private: 42}
}
在 main/main.mbt
中,我们试图创建一个类型为 R
的值:
let r : @lib.R = { x: 1, __private: 42 }
这会在第 1 行报错:
Cannot create values of struct type @lib.R because it contains private field(s).
建议#
使用公开构造函数构造值:
let r : @lib.R = @lib.R::new(1)