E4051

E4051#

标识符被定义了两次。

MoonBit 中的相互递归的定义不应该有相同的标识符。这包括:

  • 所有的顶层定义,包括变量、函数、类型、特征等。

  • 局部的命名函数。 (报告为 E4006)

  • 局部类型。

注意,局部定义的变量不是相互递归的,所以它们可以有相同的标识符,后一个定义会遮蔽前一个。

错误示例#

  • 顶层变量定义:

    pub let a = 0
    pub let a = 1 // Error: The toplevel identifier a is declared twice: it was previously defined at ...
    
  • 顶层函数定义:

    pub fn f() -> Unit {}
    pub fn f() -> Unit {} // Error: The toplevel identifier f is declared twice: it was previously defined at ...
    
  • 顶层类型定义

    pub enum A {}
    pub struct A {} // Error: The type A is declared twice: it was previously defined at ...
    
  • 局部类型定义

    pub fn g() -> Unit {
      struct A {}
      struct A {} // Error: The local type A is declared twice: it was previously defined at ...
    }
    

建议#

将标识符改为其他的名称。

pub let a = 0
pub let b = 1

如果你希望遮蔽前一个定义,可以使用一个块并将定义放在块中。

pub let a = {
  let a = 0
  1
}