Scala中的自我类型不匹配

时间:2010-05-11 13:00:02

标签: scala

鉴于此:

abstract class ViewPresenterPair {
  type V <: View 
  type P <: Presenter

  trait View {self: V =>
    val presenter: P
  }

  trait Presenter {self: P =>
    var view: V
  }
}

我试图以这种方式定义一个实现:

case class SensorViewPresenter[T] extends ViewPresenterPair {
  type V = SensorView[T]
  type P = SensorPresenter[T]

  trait SensorView[T] extends View {
  }

  class SensorViewImpl[T](val presenter: P) extends SensorView[T] {
    presenter.view = this
  }

  class SensorPresenter[T] extends Presenter {
    var view: V
  }
}

这给了我以下错误:

error: illegal inheritance;
 self-type SensorViewPresenter.this.SensorView[T] does not conform to SensorViewPresenter.this.View's selftype SensorViewPresenter.this.V
         trait SensorView[T] extends View {
                                     ^
<console>:13: error: type mismatch;
 found   : SensorViewPresenter.this.SensorViewImpl[T]
 required: SensorViewPresenter.this.V
        presenter.view = this
                         ^
<console>:16: error: illegal inheritance;
 self-type SensorViewPresenter.this.SensorPresenter[T] does not conform to SensorViewPresenter.this.Presenter's selftype SensorViewPresenter.this.P
         class SensorPresenter[T] extends Presenter {
                                          ^

我不明白为什么。毕竟,V只是SensorView[T]的别名,路径是相同的,所以它怎么能不符合呢?

1 个答案:

答案 0 :(得分:3)

找到它:当然,泛型类型中的参数T是不同的。 所以我应该写了

case class SensorViewPresenter[T] extends ViewPresenterPair {
  type V = SensorView
  type P = SensorPresenter

  trait SensorView extends View {
  }

  class SensorViewImpl(val presenter: P) extends SensorView {
    presenter.view = this
  }

  class SensorPresenter extends Presenter {
    var view: V
  }
}