E4130

E4130#

derive is not allowed for type alias.

Aliases of a type can be used interchangeably with the type itself, so it is not possible to derive traits for an alias without also deriving them for the type it aliases. Therefore, MoonBit disallows deriving traits for type aliases. Instead, one should derive the traits for the type itself.

错误示例#

struct MyStruct {
  field: Int
}

typealias StructAlias = MyStruct derive(Show) // Error: `derive` is not allowed for type alias

建议#

Move the derive attribute to the type itself:

struct MyStruct {
  field: Int
} derive(Show)

typealias StructAlias = MyStruct // Remove `derive`

If you do not have control over the type, you can create a new type that wraps the original type. However, deriving traits for a new type requires the wrapped type to also implement the trait, which is likely not the case if you were using a type alias in the first place. In this case, you will need to implement the trait manually.

type StructWrapper MyStruct

impl Show for StructWrapper with output(self, logger) {
  ...
}