Java to Kotlin Translation

I want to go to the old Kotlin Java project and found it interesting, I can not translate this to Kotlin without pain.

public interface BaseJView<P extends BaseJPresenter> {
    P createPresenter();
}
public interface BaseJPresenter<V extends BaseJView> {
    void bindView(V view);
}

Can you give advice on how I can achieve this?

+4
source share
1 answer

One way would be to use a recursive type definition like this:

interface BaseJView<TSelf : BaseJView<TSelf, P>, P : BaseJPresenter<P, TSelf>> {
    fun createPresenter(): P
}

interface BaseJPresenter<TSelf : BaseJPresenter<TSelf, V>, V : BaseJView<V, TSelf>> {
    fun bindView(view: V)
}

Then you can:

class Presenter : BaseJPresenter<Presenter, View> {
    override fun bindView(view: View) { ... }
}
class View : BaseJView<View, Presenter> {
    override fun createPresenter(): Presenter { ... }
}
+4
source

Source: https://habr.com/ru/post/1663728/


All Articles