E4113

E4113#

Constructor has no field with the given name.

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:

  • Misspelling a field name

  • Trying to access a field that exists in a different constructor

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

Erroneous example#

///|
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.
    }
  }
}

Suggestion#

To fix this error, you can either:

  • add the missing field to the constructor

///|
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)
    }
  }
}

///|
test {
  f(A(a=1, b=true))
}
  • remove the incorrect field access