E4059#
无法为内建类型或来自其他包的类型定义公共方法。
MoonBit 只允许为当前包中定义的类型定义公共方法。
错误示例#
In package a:
///|
pub(all) struct A(Int)
In package b:
///|
pub fn @a.A::f(self : @a.A) -> Int {
ignore(self)
0
}
为内建类型或标准库中的类型定义公共方法,是此错误的另一个常见情况:
///|
pub fn Int::g(self : Int) -> Int {
self
}
建议#
你可以将类型定义移动到当前包:
///|
priv struct A(Int)
///|
fn A::f(self : A) -> Int {
self.0
}
///|
test {
let value = A(1)
inspect(value.f(), content="1")
}
或者使用包装类型包裹内建类型或来自其他包的类型:
///|
priv struct WrapA(@a.A)
///|
fn WrapA::f(self : WrapA) -> Int {
let _ = self.0
0
}
///|
test {
let value = WrapA(@a.A(1))
inspect(value.f(), content="0")
}