7

形状列表推断为,List[Shape]但盒装形状列表推断为List[Box[Square | Circle]]

scala> sealed trait Shape
     | case class Square() extends Shape
     | case class Circle() extends Shape
     | case class Box[+T <: Shape](t: T)
     | List(Square(), Circle())
     | List(Box(Square()), Box(Circle()))
val res0: List[Shape & Product & Serializable] = List(Square(), Circle())
val res1: List[Box[Square | Circle]] = List(Box(Square()), Box(Circle()))

为什么res0不键入与List[Square | Circle]对称List[Box[Square | Circle]],反之亦然?

Dotty根据联合类型定义最小上限

一组类型的最小上界 (lub) 是这些类型的并集。这取代了Scala 2 规范中最小上限的定义。

与此变化相关的统一规则是什么?

4

0 回答 0