Suneet Agrawal
1 min readFeb 22, 2018

--

Aviv Callander Yes, it is possible.

You can replace the below code.

fun <T> ArrayList<T>.filterOnCondition(condition: (T) -> Boolean): ArrayList<T>{
val result = arrayListOf<T>()
for (item in this){ if(::isMultipleOf.call(item, 5)){
result.add(item)
}
}
return result
}

But the limitation here is you don’t have the access to the variables passed in isMultipleOf method as an argument from the method call (5 in our case).

There is even another way where you can call a method by its reference but that won’t work in our case.

fun checkCondition (): Boolean {
//some condition
return true
}
fun <T> ArrayList<T>.filterOnCondition(condition: (T) -> Boolean): ArrayList<T>{
val result = arrayListOf<T>()
for (item in this){ if((::checkCondition)()){
result.add(item)
}
}
return result
}

If we try to call isMultipleOf method with this approach, there will be a type miss-match issue as isMultipleOf method expects int as its first parameter whereas the data type we have in our filterOnCondition method is of Template <T> type.

--

--