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
错误示例#
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:
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)
}
}
}
remove the incorrect field access