【问题标题】:Recursive type parameters in kotlinkotlin 中的递归类型参数
【发布时间】:2019-01-22 09:18:25
【问题描述】:

我想用 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 = ...
}

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

【问题讨论】:

    标签: generics kotlin


    【解决方案1】:

    标准方式(称为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>> { ... }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      相关资源
      最近更新 更多