omensight
omensight

Reputation: 130

Why I can't use a Class type as a generic parameter in Kotlin?

I am working in an Android application using Kotlin, but when I try to use a concrete class as a fourth parameter I got an error, my question is, what I am doing wrong?

This is the base adapter of the RecyclerView

abstract class BaseAdapter<K, T: DbEntity<K>, VDB: ViewDataBinding, VH: BaseViewHolder<K,DbEntity<K>, VDB>>: RecyclerView.Adapter<VH>(){
    val items: MutableList<T> = ArrayList()

    fun addNewItems(newItems: List<T>){

    }
}

This is the class that I use for specify a generic parameter and I get an error

class CaseByCountryViewHolder(mDataBinding: ItemCaseByCountryBinding): BaseViewHolder<Int, CaseByCountry, ItemCaseByCountryBinding>(mDataBinding) {

    override fun bind(item: CaseByCountry) {
    }
}

This is the Base ViewHolder class:

abstract class BaseViewHolder<K, T: DbEntity<K>, VDB: ViewDataBinding>(mDataBinding: ViewDataBinding)
    :RecyclerView.ViewHolder(mDataBinding.root){
    protected val context: Context = mDataBinding.root.context
    protected val layoutInflater: LayoutInflater = LayoutInflater.from(context)
    abstract fun bind(item: T)
}

And finally, this is the error i get: Error

Can you help me please? I don't know what I am doing wrong, thanks in advance.

Upvotes: 1

Views: 174

Answers (1)

Akshay Nandwana
Akshay Nandwana

Reputation: 1292

It was just an argument passing error.

abstract class BaseAdapter<K, t : A<K>, 
VDB: ViewDataBinding, VH: BaseViewHolder<K,t, VDB>>{ }

class CaseByCountryAdapter() 
: BaseAdapter<Int, CaseByCountry, ViewDataBinding, CaseByCountryViewHolder>()

abstract class BaseViewHolder<K, t : A<K>, VDB: ViewDataBinding> {}

class CaseByCountryViewHolder(mDataBinding: ItemCaseByCountryBinding)
: BaseViewHolder<Int, CaseByCountry, ViewDataBinding>() {}

Upvotes: 1

Related Questions