If I have a function with overloading, how can I g...
# reflect
b
If I have a function with overloading, how can I get a
KFunction
reference to one of the overloads? I found I can use
val ref: (String) -> Unit
, for instance, as a way to disambiguate but the resulting type is not
KFunction
. It seems like I can cast this without everything crashing, but it's messy to use and not sure how stable that is.
s
If you have a reference like that, there's an experimental
reflect()
function that should get you a KFunction from it. That's the first thing that comes to mind; there might be a better solution
e
using some generic type intersection trickery, https://pl.kotl.in/wTyEg9wVR involves no casting
👏 1
very nice 1
(nor full reflection, which isn't available on playground)
b
Using
where
is a good trick there; I came up with the rest of it as well but still needed a raw cast to
KFunction
. It's still a little messy due to requiring
_
for the return type (TypeScript's infer comes to mind here), but overall works out. I had tried using
T : Function<R>
to allow the generic type to be written as a lambda (also effectively working for any arity) but it doesn't quite work because Kotlin doesn't allow multiple restrictions in
where
if one of them is another generic type.
e