具有协方差的交叉点类型
考虑以下问题:
trait AA {
def children: List[AA]
}
trait BB {
def children: List[BB]
}
class CC extends AA, BB {
override def children: List[AA] & List[BB] = ???
}
当我们覆盖childrenin 时CC,被覆盖的方法是顶级方法的合并实体。因此返回类型List[AA] & List[BB]是有道理的。
我不明白的是,下面是如何编译的?
class DD extends AA, BB {
override def children: List[AA & BB] = ???
}
List 是协变的,因此(这是证明的来源):
List[AA & BB] <: List[AA] & List[BB]
DD只能编译如果还 List[AA] & List[BB] <: List[AA & BB]。这是真的吗?如果是这样,那么不是List[AA] & List[BB] =:= List[AA & BB]。请建议
在我看来List[AA & BB] =:= List[AA] & List[BB]。考虑一下:
val xx: List[AA & BB] = ???
val yy: List[AA] & List[BB] = ???
val z1: List[AA] & List[BB] = xx
val z2: List[AA & BB] = yy
回答
你写的 DD 编译,List[AA] & List[BB]必须是List[AA & BB]. 我不明白你为什么这么认为,事实上你错了。方法返回类型是协变的,因此正好相反:List[AA & BB]必须是List[AA] & List[BB]. 而且确实是这样,所以代码没问题。