E4113

E4113#

构造函数没有给定名称的字段。

This error occurs when trying to access a field that doesn’t exist in a constructor pattern. In MoonBit, when pattern matching with constructors that have named fields, you can only access fields that were defined in the constructor’s declaration.

This commonly happens when:

  • 拼错字段名

  • 试图访问存在于其他构造函数中的字段

  • Trying to access a field that was removed or renamed in the type definition

错误示例#

pub enum E {
  A(a~: Int)
}

pub fn f(x: E) -> Unit {
  match x {
    A(..) as a => {
      println(a.a)
      println(a.b)
//            ^^^ Error: Constructor A of type E has no field b.
    }
  }
}

建议#

To fix this error, you can either:

  • 将缺少的字段添加到构造函数中

pub enum E {
  A(a~: Int, b~: Bool)
}

pub fn f(x: E) -> Unit {
  match x {
    A(..) as a => {
      println(a.a)
      println(a.b)
    }
  }
}
  • 删除不正确的字段访问