tags:

views:

77

answers:

1

Given this:

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

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

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

I am trying to define an implementation in this way:

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

Which gives me the following errors:

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 {
                                          ^

I don't understand why. After all, V is just an alias for SensorView[T], and the paths are the same, so how can it not conform?

+2  A: 

Found it: of course, the parameters T in the generic types are different. So I should have written instead

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
  }
}
Alexey Romanov