E4059

E4059#

Cannot define public methods for builtin types or types that come from other packages.

MoonBit only allows defining public methods for types that are defined in the current package.

Erroneous example#

In package a:

///|
pub(all) struct A(Int)

In package b:

///|
pub fn @a.A::f(self : @a.A) -> Int {
  ignore(self)
  0
}

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

///|
pub fn Int::g(self : Int) -> Int {
  self
}

Suggestion#

You can either move the type definition to the current package:

///|
priv struct A(Int)

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

///|
test {
  let value = A(1)
  inspect(value.f(), content="1")
}

Or use a wrapper type around the builtin type or type from another package:

///|
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")
}