E4161

E4161#

Compiler diagnostic name: mismatch_with_declare.

Declaration does not match its implementation.

This error happens when a declare item and the item that implements it describe different APIs. MoonBit checks declarations against implementations so that a forward declaration, abstract type declaration, or declared implementation relation cannot silently drift from the actual definition.

For functions and methods, the implementation must match the declared name, visibility, type parameters, parameter types, return type, constraints, and effects. For declared types, the implementation must match the declared visibility and type parameter list. For declared implementation relations, the implemented trait, target type, visibility, and type parameters must match.

Erroneous example#

declare pub fn declared_increment(x : Int) -> Int

pub fn declared_increment(x : Int) -> Double {
  x.to_double()
}

The declaration says declared_increment returns Int, but the implementation returns Double.

Suggestion#

Change either the declaration or the implementation so that the public contract is written only one way.

declare pub fn declared_increment(x : Int) -> Int

pub fn declared_increment(x : Int) -> Int {
  x + 1
}