【问题标题】:Ambiguous reference to overloaded definition对重载定义的模糊引用
【发布时间】:2015-09-06 21:21:56
【问题描述】:

以下内容:

object SomeObj {
  def addVertex(cc: Product): String = ???
  def addVertex(cc: AnyRef): String = ???
}

case class Toto(s: String)

SomeObj.addVertex(Toto(""))

正在做:

Error:(8, 10) ambiguous reference to overloaded definition,
both method addVertex in object SomeObj of type (cc: Object)String
and  method addVertex in object SomeObj of type (cc: Product)String
match argument types (A$A34.this.Toto)
SomeObj.addVertex(Toto(""));}
        ^

为什么?它不应该针对最具体的吗?
有趣的是,使用 Any 而不是 AnyRef 它可以工作。

干杯

【问题讨论】:

  • 不是真的,你的例子对我有用,没有提到的错误。您使用的是哪个版本的 Scala?

标签: scala


【解决方案1】:
trait Product extends Any with Equals 
trait Equals extends Any

在这里你可以看到Product 没有扩展AnyRef 也不是从它派生的,所以它并不比AnyRef 更具体,编译器无法在其中选择。但是ProductAny 的子类,因此它比Any 更具体,这就是您的另一个变体编译成功的原因。

【讨论】:

    猜你喜欢
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 2019-01-17
    相关资源
    最近更新 更多