这编译:
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
}
但如果删除了向上转换,则无法编译:
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
is a SortedSetLike[A, This]
,所以向上转换当然是有效的 - 但这是否表明编译器允许发生冲突的继承?
The 类似排序集特质继承了empty方法来自SetLike.
/** The empty set of the same type as this set
* @return an empty set of type `This`.
*/
def empty: This
But 排序集覆盖empty方法并具有显式返回类型:
/** Needs to be overridden in subclasses. */
override def empty: SortedSet[A] = SortedSet.empty[A]
既然你指定了This是一个子类排序集编译器会发现排序集的实施empty首先,它返回一个排序集。编译器不知道如何转换结果排序集给你的This子类。
但如果你升级到类似排序集编译器会找到它的特征empty方法返回一个This.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)