There is also another generics problem where types...
# arrow-meta
a
There is also another generics problem where types are erased and replaced with their upper bounds. So if there is a property of type
T
, it is replaced with
kotlin.Any
, if it's of type
T : List<*>
, it's of type
List<kotlin.Any>
. I know that type-erasure is a thing, but what if I want to get the generic type (
T
) where the type is generic, and the regular fq name when it is not? Is there a way to check if a type is a generic one?
r
if you have a KotlinType I believe a
isTypeVariable
or similar method is available
Type erasure should not be a thing for you in the compiler because type erasure occurs in the late codegen backend phases where you can’t do much. As for third party libs in the classpath, despite having their generics erased at runtime they still contain all the metadata information for the compiler to infer the right types from those deserialized descriptors.
That is, the compiler knows
A.identity(): A
is generic even though the runtime would just have a
Any?.identity(): Any?
function
to retain runtime generics for runtime checks like
is A
then you need inline reified generics, those the compiler capture and are available tagged for runtime checks
a
Useful stuff! Thank you!
I believe a 
isTypeVariable
 or similar method is available
There exists
KotlinType#isCustomTypeVariable
I guess this's the one you mean.
That is, the compiler knows 
A.identity(): A
 is generic even though the runtime would just have a 
Any?.identity(): Any?
 function
The problem is I was again using
KotlinType#getJetTypeFqName
and It would just return the upper bound of said generic type, does the type need to be inline reified in order for this to work?
Just found
KotlinType#isTypeParameter
and it does seem to do the trick, I'm then able to get the text of the generic type by simply calling
KotlinType#toString
r
awesome!
K 1
arrow 1