2

I have an abstract function, where I want to get any of my Activity Presenters in BaseActivity

protected abstract fun <T : BasePresenter<V>, V : BaseView> getPresenter(): T

So my function shoud accept only class that extends BasePresenter with view that extands BaseView

But when I implimment this function, I get an error

private lateinit var presenter: LauncherPresenter

override fun <T : BasePresenter<V>, V : BaseView> getPresenter(): T = presenter

Type mismatch.

Required: T

Found: LauncherPresenter

I know this is stupid question, but I cant get where I am wrong.

Sujatha Girijala
  • 1,141
  • 8
  • 20
Anton A.
  • 1,718
  • 15
  • 37

2 Answers2

3

You don't need to declare generic in your function, because it already specified in your BaseActivity at the top.

Example your BaseActivity:

abstract class BaseActivity<T : BasePresenter<V>, V : BaseView>() : 
AppCompatActivity(), BaseView {

  protected abstract fun getPresenter(): T

}

Where T specified at the top.

When you implement function in child of BaseActivity:

class LauncherActivity() : 
BaseActivity<LauncherPresenter<LauncherView>, LauncherView>(), LauncherView {

     private lateinit var presenter: LauncherPresenter

     //your override method
     protected override fun getPresenter(): LauncherPresenter = presenter

}
KolinLoures
  • 130
  • 2
  • 10
1

You can use your code but with Unchecked cast like:

override fun <T : BasePresenter<V>, V : BaseView> getPresenter(): T = presenter as T

LauncherPresenter can't become T itself in the form that you use

Stanislav Bondar
  • 6,056
  • 2
  • 34
  • 46