Search code examples
androidgenericskotlinkotlin-android-extensionskotlin-extension

Kotlin function required Nothing but defined as a different type


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?


Solution

  • 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.