Scala中的联合类型子类型

时间:2016-07-06 13:07:17

标签: scala types dotty

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

1 个答案:

答案 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}