Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Derive function reference from a generic extension function/method in Kotlin

Is there a way to get a function reference to a generic extension function/method in Kotlin?

class C
fun <T> C.f1(t: T) = TODO()
val f1Ref1 = C::f1<T> // Compiler error
val f1Ref2 = C::f1 // Compiler error

But this is fine:

fun C.f2(i: Int) = TODO()
val f2Ref = C::f2
like image 556
Julian A. Avatar asked May 11 '26 17:05

Julian A.


1 Answers

You must specify the type explicitly

val f1Ref1: C.(Int) -> Unit = C::f1
like image 55
IR42 Avatar answered May 14 '26 11:05

IR42