E4145#
无法实现封闭特征。
封闭的特征不允许在其他包中实现。使用 pub(open) 使该特性对其他包开放,以便实现。
错误示例#
例如,在模块 username/hello 中存在一个包 a:
a/moon.pkg:
a/a.mbt:
pub trait Sealed {
to_int(Self) -> Int
}
而在包 b 中:
b/moon.pkg:
import {
"username/hello/a" @a,
}
b/b.mbt:
pub struct A {
value : Int
}
impl @a.Sealed for A with to_int(self : A) -> Int { // Error: Cannot implement trait '@a.Sealed' because it is readonly.
self.value
}
pub fn make(value : Int) -> A {
{ value, }
}
建议#
您可以将该特性的可见性更改为 pub(open),以便其他包可以实现它。
pub(open) trait Sealed {
to_int(Self) -> Int
}