E4175

E4175#

Compiler diagnostic name: as_not_trait_object.

The right-hand side of as is not a trait object type.

MoonBit uses as &Trait to convert a value to a trait object. The target type must therefore be a trait object type such as &Show, not an ordinary concrete type, type parameter, or package type.

Erroneous example#

fn invalid_cast() -> Unit {
  let _ = 1 as Int
}

Int is a concrete type, not a trait object type, so it cannot be used on the right-hand side of as.

Suggestion#

Use as &Trait when you want a trait object. If you only need a type annotation, write the expected type as an annotation instead of using as.

fn show_value() -> Unit {
  let value = 1 as &Show
  println(value)
}