E4059

E4059#

无法为内建类型或来自其他包的类型定义方法。

MoonBit 只允许为当前包中定义的类型定义方法或实现特征(trait)。

错误示例#

In package a:

pub type A Int

In package b:

fn f(self : @a.A) -> Int { // Error: Cannot define method f for type @a.A from package a
  0
}

Defining methods for builtin types, or types in standard library, is another frequent case of this error:

fn f(self : Int) -> Int { // Error: Cannot define method f for builtin type Int
  0
}

建议#

你可以将类型定义移动到当前包:

pub type A Int

fn f(self : A) -> Int {
  0
}

或者使用新类型将内建类型或来自其他包的类型包装起来:

pub type WrapA @a.A

fn f(self : WrapA) -> Int {
  ignore(self._) // Use `._` to access the wrapped value if @a.A is public
  0
}