Kotlin - declare a generic class with a type parameter that is generic
So I have a BasePresenter class. BaseMvpView is an interface
open class BaseMvpPresenter<View : BaseMvpView> {
}
I am trying to create a BaseMvpActivity class that is parameterized using BaseMvpPresenter like this:
abstract class BaseMvpActivity<T : BaseMvpPresenter> : BaseActivity(), BaseMvpView {
}
But I am getting the following error:
One type argument expected for class BaseMvpPresenter<View: BaseMvpView>
How do I correctly declare this? I want to be able to use BaseMvpActivity like this:
abstract class BaseMvpActivity<T : BaseMvpPresenter<U>, U : BaseMvpView> : BaseActivity(), BaseMvpView {
abstract var presenter: T
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
presenter.onViewCreated(this)
}
override fun onDestroy() {
presenter.onViewDestroyed()
super.onDestroy()
}
}
+3
source to share
1 answer
You need to pass a parameter to BaseMVPPresenter
. You can add second parameter to your abstract class like this
abstract class BaseMvpActivity<V: BaseMvpView, T : BaseMvpPresenter<V>> : BaseActivity(), BaseMvpView {
}
so let's say that you are creating a view to show messages, then you would do something like this:
class Activity : BaseMVPActivity<PostsView, PostPresenter<PostView>() {
//Here comes your code
}
Hope it helps :)
+4
source to share