E4063#

尽管定义了 impl,但类型未实现特征。

此误差可进一步分为 5 种情况:

  • 缺少方法

  • 实现细节

  • 方法类型不匹配

  • 不满足约束条件

  • 方法包含未解析的类型参数

错误示例#

缺少方法#

///|
trait Number {
  to_int(Self) -> Int
  to_float(Self) -> Float
}

///|
struct A(Int)

///|
impl Number for A with to_int(self : A) -> Int {
  //^~~~~~~~~~~~~~~
  // Error: Type A does not implement trait Number, although an `impl` is defined.
  // hint:
  //   method to_float is missing.
  self.0
}

建议#

根据错误信息提供的提示修改代码。对于上面的示例,你可以将缺少的方法 to_float 添加到类型 A

///|
priv trait Number {
  to_int(Self) -> Int
  to_float(Self) -> Float
}

///|
priv struct A(Int)

///|
impl Number for A with to_int(self : A) -> Int {
  self.0
}

///|
impl Number for A with to_float(self : A) -> Float {
  Float::from_int(self.0)
}

///|
test {
  let value : A = A(42)
  inspect(value.to_int(), content="42")
  ignore(value.to_float())
}