is it possible to determine if a lambda type has a...
# reflect
j
is it possible to determine if a lambda type has a receiver or just a normal argument from parsing a function parameter's type? i'm in the process of switching from reflect to metadata-jvm (or maybe fully blown UAST) but looking for a quick workaround until then
The
KParameterImpl
has the
KFunctionImpl
in its private
callable
property, but I can't get access to that as far as I can tell
Oh nevermind that's the enclosing function
e
without metadata, I believe
(T) -> R
and
T.() -> R
are represented by the same
kotlin.Function1<T, R>
which is erased to
kotlin.jvm.internal.Function1
on JVM: https://github.com/JetBrains/kotlin/blob/master/spec-docs/function-types.md
j
They are, but kotlin-reflect uses metadata, right? That's why it's like 20MiB or whatever because it embeds all the protos.
e
oh I thought you were looking for a way to do it without metadata-jvm. hmm, seems like you should be able to do it with metadata but it's not obvious to me how
j
yeah i think i just need to fully abandon "runtime" reflection and switch to metadata-jvm parsing of the class files
solves more problems than just this current one
e
Copy code
kotlin.reflect.typeOf<Unit.() -> Unit>() == kotlin.reflect.typeOf<(Unit) -> Unit>()
I don't think kotlin-reflect exposes any difference :-/
j
I think you can!
Copy code
type.annotations.any { it.annotationClass == ExtensionFunctionType::class }
e
Copy code
>>> fun f(block: (Unit) -> Unit) = Unit
>>> fun g(block: Unit.() -> Unit) = Unit
>>> ::f.parameters.single().type.annotations
res0: List<Annotation> = []
>>> ::g.parameters.single().type.annotations
res1: List<Annotation> = [@kotlin.ExtensionFunctionType()]
>>> ::f.parameters.single().type == ::g.parameters.single().type
res2: Boolean = true
>>> typeOf<Unit.() -> Unit>().annotations
res3: List<Annotation> = []
… ok, I guess that works, but it's kinda surprising
j
e
looks like there's also
@kotlin.ContextFunctionTypeParams(count: Int)
and
@kotlin.ParameterName(name: String)
(the latter on the type parameters to
FunctionN<>
), and the lambda type also seems to know if it's a
suspend
function, but I'm not sure how that's determined
that's actually a pretty decent amount of information without having to pull kotlinx-metadata-jvm yourself