E4203

E4203#

Compiler diagnostic name: constr_as_function.

MoonBit does not allow constructors to be passed directly as higher-order functions. A constructor may still be called normally, but when a function value is required, wrap the constructor call in a lambda so the argument flow is explicit.

Erroneous example#

///|
priv enum Message {
  Text(String)
}

///|
fn build(make : (String) -> Message) -> Message {
  make("ready")
}

///|
test {
  let _ = build(Text)
}

Suggestion#

Use a lambda that calls the constructor.

///|
priv enum Message {
  Text(String)
}

///|
fn build(make : (String) -> Message) -> Message {
  make("ready")
}

///|
test {
  let message = build(value => Text(value))
  match message {
    Text(value) => inspect(value, content="ready")
  }
}