Extension of the general class in Kotlin

Let's say that I have this sample code in Java:

public class MyActivityDelegate implements ActivityMvpDelegate 

where ActivityMvpDelegate:

 interface ActivityMvpDelegate<V extends MvpView, P extends MvpPresenter<V>> 

The same code converted to Kotlin is as follows

 class MyActivityDelegate(private val activity: Activity) : ActivityMvpDelegate<MvpView, MvpPresenter<V>> 

Of course, I got an unresolved link in V , and I'm not sure how this code should look like, in Java I don't need to specify the general text here .. any advice would be greatly appreciated

+5
source share
1 answer

The declaration of your interface requires that

  • V extends MvpView
  • V (exactly V , not its subtype) is used as a general parameter for P extends MvpPresenter<V>

Given that you cannot extend ActivityMvpDelegate<MvpView, MvpPresenter<V>> , because there is no guarantee that V is exactly MvpView (also, in Kotlin, common parameters are not implicitly inherited, you need to redo them as class SomeClass<T> : SomeInterface<T> ).

You can, however, write it as

 class MyActivityDelegate(private val activity: Activity) : ActivityMvpDelegate<MvpView, MvpPresenter<MvpView>> 

or enter another general parameter, so that V and the argument for P still the same:

 class MyActivityDelegate<T : MvpView>(private val activity: Activity) : ActivityMvpDelegate<T, MvpPresenter<T>> 

You can also change the general declaration of your interface from P extends MvpPresenter<V> to P extends MvpPresenter<? extends V> P extends MvpPresenter<? extends V> (or use out V in Kotlin), and you can use any subtype V as an argument, including a limited pedigree:

 class MyActivityDelegate<T : MvpView>(private val activity: Activity) : ActivityMvpDelegate<MvpView, MvpPresenter<T>> 
+4
source

All Articles