E4131

E4131#

类型别名是一个函数类型,不是一个类型构造器。

当您尝试为类型别名定义方法时,会发生此错误。类型的别名可以与类型本身互换使用,不能为函数类型定义方法。因此,MoonBit 禁止为函数类型的类型别名定义方法。

错误示例#

pub type FuncAlias = (Int) -> Unit

pub fn FuncAlias::call(self : FuncAlias) -> Unit {
  //   ^~~~~~~~~
  // Error: The type alias FuncAlias is a function type, not a type constructor.
  self(0)
}

建议#

如果您想为函数类型定义方法,您应该定义一个新类型,它包装函数类型:

pub struct FuncWrap((Int) -> Unit)

pub fn FuncWrap::call(self : FuncWrap) -> Unit {
  self.0(0)
}

pub fn make(f : (Int) -> Unit) -> FuncWrap {
  FuncWrap(f)
}