E4145

E4145#

无法实现封闭特征。

封闭的特征不允许在其他包中实现。使用 pub(open) 使该特性对其他包开放,以便实现。

错误示例#

例如,在模块 username/hello 中存在一个包 a

a/moon.pkg.json:

{}

a/a.mbt:

pub trait Sealed {
  to_int() -> Int
}

而在包 b 中:

b/moon.pkg.json:

{
  "import": [
    "username/hello/a"
  ]
}

b/b.mbt:

type A Int

impl @a.Sealed for A with to_int(self : A) -> Int { // Error: Cannot implement trait '@a.Sealed' because it is readonly.
  self._
}

建议#

您可以将该特性的可见性更改为 pub(open),以便其他包可以实现它。

pub(open) trait Sealed {
  to_int() -> Int
}