我可以轻松地使用Dotty:
trait Ex {type T <: Int | Seq[Int]; def f:T}
trait Ex2 extends Ex {override type T = Seq[Int]; override def f = Seq(2)}
trait Ex3 extends Ex {override type T = Int; override def f = 2}
如何在没有Dotty的情况下进行联合类型子类型化?
答案 0 :(得分:1)
简单的解决方案是上限类型:
trait Ex {type T >: Int with Seq[Int]; def f:T}
trait Ex2 extends Ex {override type T = Seq[Int]; override def f = Seq(2)}
trait Ex3 extends Ex {override type T = Int; override def f = 2}