【发布时间】:2015-06-26 18:58:50
【问题描述】:
这样编译:
import scala.collection._
trait Foo[A, +This <: SortedSet[A] with SortedSetLike[A,This]]
extends SortedSetLike[A, This] { this: This =>
def bar: This = (this: SortedSetLike[A,This]).empty
}
但是如果删除了upcast,它就无法编译:
import scala.collection._
trait Foo[A, +This <: SortedSet[A] with SortedSetLike[A,This]]
extends SortedSetLike[A, This] { this: This =>
def bar: This = this.empty
}
为什么?从extends 子句中,我们知道Foo 是SortedSetLike[A, This],所以向上转换当然是有效的——但这是否表明编译器允许发生冲突继承?
【问题讨论】:
-
我不知道这个案例的确切细节,但这是另一个可能发生的可怕事情的例子,因为子类中的方法可以具有比它们实现的方法签名更具体的返回类型。跨度>
标签: scala scala-collections upcasting