【发布时间】:2021-04-09 08:52:00
【问题描述】:
考虑以下内容:
trait AA {
def children: List[AA]
}
trait BB {
def children: List[BB]
}
class CC extends AA, BB {
override def children: List[AA] & List[BB] = ???
}
当我们在CC 中覆盖children 时,被覆盖的方法是顶级方法的合并实体。因此返回类型 List[AA] & List[BB] 是有意义的。
我不明白的是,下面是怎么编译的?
class DD extends AA, BB {
override def children: List[AA & BB] = ???
}
List 是协变的,因此(这里是 proof 的来源):
List[AA & BB] <: List[AA] & List[BB]
DD 只有在 also 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
【问题讨论】:
标签: scala generics dotty scala-3