E4053#
编译器诊断名称:invalid_self_type。
无效的 “self” 类型:必须是类型构造器。
当你试图为一个不是类型构造器的类型定义方法或实现 trait 时,会出现这个错误。
属于类型构造器的类型:
元组:
(Int, Bool)enums,structs,traits, new types (type), and error types (type!).
不属于类型构造器的类型:
函数类型:
(Int) -> Bool类型参数:如
fn f[T](x : T) -> T中的T
错误示例#
pub(open) trait T {
f(Self) -> Unit
}
impl T for (Int) -> Unit with f(self) {
// ^
// Error: Invalid type for "self": must be a type constructor.
self(0)
}
建议#
请使用结构体、枚举、trait 或新类型等类型构造器。如果需要为函数值附加方法,请先用具名类型包装该函数。
pub(open) trait T {
f(Self) -> Unit
}
pub struct Handler {
run : (Int) -> Unit
}
impl T for Handler with f(self) {
(self.run)(0)
}
fn init {
Handler::{ run: x => ignore(x) }.f()
}