I m doing something about the android DiffUtil and i want to make it generic.I want to pass the getter function (itemGetter) when creating the class A but when i declare the type of itemGetter in the constructor. I don't know how to declare it. ()->V is incorrect. because I can't doing something like item.itemGetter() The Class T is about a class with a itemGetter
class T() {
val item = ....
fun itemGetter(): String {
return this.item
}
}
class A<T,V>(var item1 :T , var item2:T ,var itemGetter: ()->V){
fun comparing(item1: T,item2:T): Boolean {
return item1.itemGetter()===item2.itemGetter()
}
}
Thanks
update specific Example
class DiffCallBackUtil<T>(var oldList: ArrayList<T>, var newList: ArrayList<T> ) : DiffUtil.Callback() {
override fun areItemsTheSame(oldi: Int, newi: Int): Boolean {
return oldList[oldi] === newList[newi]
}
}
Because we check the are itemsTheSame require to compare the name or id or
something instance in the model T, Therefore, i need pass a getter as function in the Model T to the DiffCallBackUtil.
Therefore, i can invoke oldList[oldi].getName()===newList[newi].getName()