E4017

E4017#

方法的类型有歧义,可能来自多个特征。

错误示例:#

priv struct S {
  v : Int
}

priv trait A {
  value(Self) -> Int
}

priv trait B {
  value(Self) -> Int
}

impl A for S with value(self) {
  self.v
}

impl B for S with value(self) {
  self.v + 1
}

pub fn read() -> Int {
  let s : S = { v: 3 }
  s.value()
}

上述例子中,试图在类型 S 上调用方法 value,但方法名称来自 AB 两个特征,会产生类似如下的错误:

类型 S 的方法 value 有歧义,它可能来自 trait A 或 B

建议#

通过指明具体的特征对方法去歧义:

priv struct S {
  v : Int
}

priv trait A {
  value(Self) -> Int
}

priv trait B {
  value(Self) -> Int
}

impl A for S with value(self) {
  self.v
}

impl B for S with value(self) {
  self.v + 1
}

pub fn read() -> Int {
  let s : S = { v: 3 }
  A::value(s) + B::value(s)
}