In the following code,
class IncWrapper<T> (val wrapped: T, val base: Int) {
fun incFunction(increment: Int, func: T.(Int) -> Int): Int {
return increment + wrapped.func(base)
}
}
class ClassWithIndecentlyLongName {
fun square(x: Int) = x * x
}
fun main() {
val wrapper = IncWrapper(ClassWithIndecentlyLongName(), 2)
val computed = wrapper.incFunction(1, ClassWithIndecentlyLongName::square)
println(computed)
}
we pass a reference to a method of the wrapped class ClassWithIndecentlyLongName
. Since it is known at the call site that this class is expected as the receiver of the method, it seems awkward / redundant to pass the name of the class again. I'd expect something like ::square
to work, but it doesn't. If such feature is missing, what might be the reasons?
(The question arose from an attempt to refactor some very wordy Java code converting lots of fields of one class to another.)