0

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?

Andrey Danilov
  • 6,194
  • 5
  • 32
  • 56

2 Answers2

1

BaseConnectionPresenter is a subtyp of BasePresenter<T> with T: BaseConnectionView. The function getPresenter only returns BasePresenter<BaseView>. There's a problem because BasePresenter<T> is not garanteed to be BasePresenter<BaseView>. The following fixes it:

class BaseConnectionPresenter<T : BaseConnectionView> : BasePresenter<BaseView>()
s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
0

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<*>
Andrey Danilov
  • 6,194
  • 5
  • 32
  • 56