【问题标题】:In scala, why type alias in a class cannot be referred for inheritance?在scala中,为什么不能引用类中的类型别名进行继承?
【发布时间】:2017-01-02 22:44:36
【问题描述】:

我定义了以下类:

文件:PyRef.scala:

package mypackage
class PyBinding() {
...
}

trait PyRef {

  type Binding = PyBinding
}

文件:Link.scala:

class Link() extends PyRef {

  override type Binding = Link.PyBindingImpl
}

object Link {
  class PyBindingImpl() extends PyRef#Binding() {
  }
}

编译时抛出如下错误:

Error:(222, 34) class type required but mypackage.PyBinding found
                 ) extends PyRef#Binding() {

为什么会发生这种情况,我应该怎么做才能避免它?我正在使用 Scala 2.10。

【问题讨论】:

标签: scala inheritance type-alias


【解决方案1】:

我认为您不能在 Scala 中使用类型投影A#B 作为可实例化的类类型。以下两件事起作用

trait Link {
  class PyBindingImpl() extends PyBinding()
}

trait Link {
  val ref: PyRef

  class PyBindingImpl() extends ref.Binding()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多