I want to choose function reference before applying it to arguments, but Kotlin cannot infer its type.
Suppose I have a class
class Circle(val radius: Float)
and a function
fun sortByRadius(circles: MutableList<Circle>, ascending: Boolean) {
if (ascending)
circles.sortBy { it.radius }
else
circles.sortByDescending { it.radius }
}
I want too rewrite this function body to something like this:
circles.(
if (ascending) MutableList<Circle>::sortBy
else MutableList<Circle>::sortByDescending
) { it.radius }
but it does not work. Also I found out that
(MutableList<Circle>::sortBy)(circles) { it.radius }
almost works, Kotlin just can not infer Float
type of radius; so I wonder how to specify it. Then we could write
(if (ascending) MutableList<Circle>::sortBy
else MutableList<Circle>::sortByDescending)(circles) { it.radius }