E0045

E0045#

Implementing traits with methods.

Initially, MoonBit allows implementing a trait implicitly with regular methods. But we intend to deprecate this behavior and allow only explicit impl declarations, to make the language simpler and clearer. Currently, this deprecation is going through a "graceful migration" period, and this warning will be emitted if your code uses implicit trait implementation.

Erroneous example#

type MyType Int

fn op_equal(self : MyType, other : MyType) -> Bool {
  self._ == other._
}

test {
  // `==` will call `Eq::op_equal`
  println(MyType(1) == MyType(2))
}

Suggestion#

Add an explicit impl declaration, or migrate existing method to impl. Notice that impl can be called with dot syntax, so migrating to impl should be basically harmless and make the intention of code clearer:

type MyType Int

impl Eq for MyType with op_equal(self, other) {
  self._ == other._
}

test {
  println(MyType(1) == MyType(2))
}