androidkotlinmoxyandroid-moxy

Kotlin: resolve generic inheritance


After reading same articles I still cannot solve generics problem:

I have BaseActivity:

abstract class BaseActivity : MvpAppCompatActivity(), BaseView {
    abstract fun getPresenter():BasePresenter<BaseView>
}

BaseView interface for it

interface BaseView : MvpView

And for sure BasePresenter

open class BasePresenter<T : BaseView> : MvpPresenter<T>() 

Then I create BaseConnectionView

interface BaseConnectionView : BaseView

And BaseConnectionPresenter

class BaseConnectionPresenter<T : BaseConnectionView> : BasePresenter<T>()

So when I create BaseConnectionActivity

abstract class BaseConnectionActivity : BaseActivity(),BaseConnectionView {
    override abstract fun getPresenter(): BaseConnectionPresenter<BaseConnectionView>
}

I have error:

Return type is BaseConnectionPresenter<BaseConnectionView>, 
which is not a subtype of overridden 
public abstract fun getPresenter():BasePresenter<BaseView>

But it is subtype!

How can I solve this problem?


Solution

  • Solution was easier than I think if use star-projections

    So in BaseActivity I replaced

    abstract fun getPresenter():BasePresenter<BaseView>
    

    To

    abstract fun getPresenter():BasePresenter<*>
    

    And then I can just override it with new presenter, like

    override abstract fun getPresenter(): BaseConnectionPresenter<*>