9

I've defined a class like so

abstract class MvpViewHolder<P>(itemView: View) : RecyclerView.ViewHolder(itemView) where P : BasePresenter<out Any?, out Any?> {
    protected var presenter: P? = null

    fun bindPresenter(presenter: P): Unit {
        this.presenter = presenter
        presenter.bindView(itemView)
    }
}

where presenter.bindView(itemView) gives me an error stating Type mismatch, required: Nothing, found: View!. I've defined the bindView inside the presenter class like so

abstract class BasePresenter<M, V> {
     var view: WeakReference<V>? = null
     var model: M? = null

     fun bindView(view: V) {
        this.view = WeakReference(view)
    }
}

It is taking in a value of view: V.

I've tried defining my extension of BasePresenter<out Any?, out Any?> using the star syntaxt BasePresenter<*,*> but I get the same error. I've also tried using just simply BasePresenter<Any?, Any?> which fixes the direct problem, but then anything that is extending P: BasePresenter<Any?, Any?> Gives an error saying that it was expecting P, but got BasePresenter<Any?, Any?>

Here is an example where that happens within my code

abstract class MvpRecyclerListAdapter<M, P : BasePresenter<Any?, Any?>, VH : MvpViewHolder<P>> : MvpRecyclerAdapter<M, P, VH>() {...}

On this line, I would get the error mentioned above on the part the extends MvpRecyclerAdapter<M, P, VH>

I just can't seem to get around this. How can I fix it?

Rafa
  • 3,219
  • 4
  • 38
  • 70

1 Answers1

5

You've declared out for generic parameter V at BasePresenter<out Any?, out Any?>, so presenter.bindView must not take input parameters.

Solution: change declaration to BasePresenter<out Any?, View?>.

Check official doc for more infomation.

Lym Zoy
  • 951
  • 10
  • 16
  • it'll still give me the error on this line `abstract class MvpRecyclerListAdapter, VH : MvpViewHolder

    > : MvpRecyclerAdapter() {...}` stating that, specifically for `P` that it `expects `P` but found BasePresenter

    – Rafa Nov 02 '17 at 01:49
  • 1
    @Rafa shouldn't usages of `P` in your generics be `P : BasePresenter` everywhere .. I mean generics are good, but you've gone to town with them here ... – Mark Nov 02 '17 at 01:57
  • You're right. and yea, I'm starting an app using MVP and I'm trying to convert this code over to Kotlin https://github.com/remind101/android-arch-sample/tree/master/app/src/main/java/com/remind101/archexample It's proven to be pretty difficult converting the generic signatures to Kotlin – Rafa Nov 02 '17 at 02:47
  • @Rafa, did you resolve these issues above, I'm trying convert the same code over to Kotlin – Phong NGUYEN Feb 25 '20 at 07:12