what’s the type of a lambda expression when seen f...
# compiler
a
what’s the type of a lambda expression when seen from Java? My actual question: is it possible to pass lambdas to Java code and retrieve them based on the type? For example, store a lambda in a Spring Context and then retrieve it using the type of the lambda expression somehow? The little digging I’ve been able to do gives me the impression that it’s not easily doable. For example,
suspend
is lost to the Java type system and seems to be compiled into coroutine-friendly code with no type info about it.
s
It is Function0-22 or FunctionN, depending on number of arguments
e
yes, they get translated to functions taking an additional continuation parameter https://github.com/JetBrains/kotlin/blob/master/spec-docs/function-types.md
a
ah, i see, so the continuation is just an argument to a FunctionN?
I suppose there’s no way to get a lambda by its type then, type erasure etc, and there’s no way to create a “named” FunctionN?
e
you can create your own
Copy code
fun interface MyLambda {
    suspend operator fun invoke(args...)
}
which isn't a
Function
but can be used in the same ways
a
hmm, good point, it’s not a raw lambda, but that would probably work