Given the following class
class Foo {
fun bar() {}
}
I can reference the bar function like so
Foo::bar
I want to be able to do this for generic functions
class Foo {
fun <T> bar() {}
}
But the compiler throws the following error
Kotlin: Type inference failed: Not enough information to infer parameter T in fun bar(): Unit Please specify it explicitly.
How can I provide the parameter for generic function references?
Workaround 1 (Function proxy)
Proxy is probably the wrong term here but until someone tells me otherwise...
One approach I've been able to do is proxy the function, removing the generic type, but this isn't a great approach if I'm honest...
fun Foo.barOfInt() = bar<Int>()
Which can be referenced normally...
Foo::barOfInt
Workaround 2 (Get by name)
The second approach that seems successful, albeit still not very clean would be to build a KClass<*> extension that can get the method by name...
fun KClass<*>.getGenericFunction(name: String): KFunction<*> {
return members.single { it.name == name } as KFunction<*>
}
Which I can use like so...
Foo::class.getGenericFunction("bar")