Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive type parameters in kotlin

I want to write something like that in 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 = ...
}

How I can properly do that?

like image 620
Andrei Tanana Avatar asked Jan 22 '19 09:01

Andrei Tanana


1 Answers

The standard way (called F-bounded polymorphism) is

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

It may make more sense to put out elsewhere here, depending on the specifics:

open class View<out V: View<V, P>, out P: Presenter<V>> { ... }
like image 193
Alexey Romanov Avatar answered Sep 25 '22 18:09

Alexey Romanov