Несоответствие типов в Scala - PullRequest
3 голосов
/ 11 мая 2010

Учитывая это:

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 Ответ

3 голосов
/ 11 мая 2010

Нашел его: конечно, параметры 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
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...