# 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](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:

  ```{literalinclude} /sources/error_codes/4051_error/top.mbt
  :language: moonbit
  ```

- Toplevel function definition:

  ```moonbit
  pub fn f() -> Unit {}
  pub fn f() -> Unit {} // Error: The toplevel identifier f is declared twice: it was previously defined at ...
  ```

- Toplevel type definition:

  ```moonbit
  pub enum A {}
  pub struct A {} // Error: The type A is declared twice: it was previously defined at ...
  ```

- Local type definition:

  ```moonbit
  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.

```{literalinclude} /sources/error_codes/4051_fixed/top.mbt
:language: moonbit
```

If you want to shadow the previous definition, you can use a block and put the
definition in the block.

```{literalinclude} /sources/error_codes/4051_fixed/top_1.mbt
:language: moonbit
```
