为什么我不能将 case 对象用作多态类型

2024-02-05

以下代码无法编译:

  case object O

  trait Show[A] {def show(a: A) : String}

  class OShow extends Show[O] {
    override def show(a: O): String = "ahoy"
  }

编译错误是

Error: not found: type O
  class OShow extends Show[O] {

那么,如何将 case 对象用作多态类型呢? ^


正如 @endeneu 提到的,对于 case 对象,您需要使用 .type,也称为单例类型注释:

class OShow extends Show[O.type] {
  override def show(a: O.type): String = "ahoy"
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我不能将 case 对象用作多态类型 的相关文章

随机推荐