Is there a way to get a `KType` from an `Any` (or...
# announcements
s
Is there a way to get a
KType
from an
Any
(or the corresponding KClass<*>) or am I stuck with only having access to the
KClass<*>
?
z
starProjectedType
?
s
Does that erase generic types?
It does sadly.
z
Well, technically the generic types are erased already at compile time, so that information doesn’t exist anymore by the time you get to reflection at runtime.
If you’re asking if there’s a way to recover generic types at runtime through reflection, there isn’t, this is JVM limitation, although you can recover type bounds if that’s helpful at all.
s
Oh yeah, that’s true. Damn. Ok I’ll look into an alternative solution.
z
Note that the
typeOf
function will give you specific type parameters, but it does that using
reified
, which means you have to actually know the full type when you call it, so you can’t use it with an instance value.