1

我可以使用 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 的情况下进行联合类型子类型化?

4

1 回答 1

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}
于 2016-07-06T16:02:14.117 回答