E4051#
The identifier is declared twice.
Mutually recursive definitions in MoonBit shall not have the same identifier. This include:
All toplevel definitions, including variables, functions, types, traits, etc.
Locally named functions. (Reported as E4006)
Local types.
Note, locally defined variables are not mutually recursive, so they can have the same identifier, and the later definition shadows the previous one.
Erroneous example#
Toplevel variable definition:
pub let a = 0 pub let a = 1 // Error: The toplevel identifier a is declared twice: it was previously defined at ...
Toplevel function definition:
pub fn f() -> Unit {} pub fn f() -> Unit {} // Error: The toplevel identifier f is declared twice: it was previously defined at ...
Toplevel type definition:
pub enum A {} pub struct A {} // Error: The type A is declared twice: it was previously defined at ...
Local type definition:
pub fn g() -> Unit { struct A {} struct A {} // Error: The local type A is declared twice: it was previously defined at ... }
Suggestion#
Rename the identifier to a different name.
pub let a = 0
pub let b = 1
If you want to shadow the previous definition, you can use a block and put the definition in the block.
pub let a = {
let a = 0
1
}