Unexpected Trait Behavior

非 Y 不嫁゛ 提交于 2019-12-02 20:37:06

It seems that singleton types without members are type-equivalent somehow here. Perhaps it's a bug (you filed a ticket). For example, the following produces a runtime-error:

sealed trait Parent
case object Boy  extends Parent
case object Girl extends Parent

trait HasGirl {
  val x: Girl.type
}

case class Thing(x: Boy.type) extends HasGirl {
  def y: Girl.type = (this: HasGirl).x
}


val t = Thing(Boy)
t.y   // ClassCastException !

If I add a member, you get a compile-time error:

sealed trait Parent
case object Boy  extends Parent
case object Girl extends Parent { def hello = 1234 }

trait HasGirl {
  val x: Girl.type
}

case class Thing(x: Boy.type) extends HasGirl
<console>:57: error: overriding value x in trait HasGirl of type Girl.type;
 value x has incompatible type
       case class Thing(x: Boy.type) extends HasGirl
                        ^
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!