Kotlin中的递归类型参数

时间:2019-01-22 09:18:26

标签: generics kotlin

我想在Kotlin中写类似的东西。

open class View<P> where P:Presenter<out _this_class_> {
    val presenter: P = ...
}

open class Presenter<V> where V: View<out _this_class_> {
    val view: V = ...
}

我该怎么做呢?

1 个答案:

答案 0 :(得分:4)

标准方式(称为F-bounded polymorphism)是

open class View<V: View<V, P>, P: Presenter<out V>> { ... }

根据具体情况,将out放在此处可能更有意义:

open class View<out V: View<V, P>, out P: Presenter<V>> { ... }