In Kotlin, I can obtain a KType
from a KClass<*>
like so:
Int::class.createType()
kotlin.Int
How do I do the reverse and obtain the KClass<Int>
from a KType
representing a kotlin.Int
?
You can use KType.classifier
for this:
val intType : KType = Int::class.createType()
val intClassifier : KClassifier? = intType.classifier
assertEquals(Int::class, intClassifier) // true
Note that since 1.3.40 you can also (at least on the JVM) use the experimental typeOf<Int>()
to get your KType
. You may want to have a look at the 1.3.40-announcement to see whether that might be useful to you.
Speaking of the JVM: on the JVM you can also use KType.jvmErasure
to get the actual class as also marstran pointed out in the comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With