kotlin 中的递归类型参数

And*_*ana 5 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 = ...
}
Run Code Online (Sandbox Code Playgroud)

我怎么能正确地做到这一点?

Ale*_*nov 6

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

open class View<V: View<V, P>, P: Presenter<out V>> { ... }
Run Code Online (Sandbox Code Playgroud)

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

open class View<out V: View<V, P>, out P: Presenter<V>> { ... }
Run Code Online (Sandbox Code Playgroud)