Kotlin - KClass<*> from KType - Printable Version +- 0Day Forums (https://zeroday.vip) +-- Forum: Coding (https://zeroday.vip/Forum-Coding) +--- Forum: Kotlin (https://zeroday.vip/Forum-Kotlin) +--- Thread: Kotlin - KClass<*> from KType (/Thread-Kotlin-KClass-lt-gt-from-KType) |
Kotlin - KClass<*> from KType - nonfrustration902665 - 07-20-2023 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`? RE: Kotlin - KClass<*> from KType - homotropal678503 - 07-20-2023 You can use [`KType.classifier`]( [To see links please register here] ) 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 see links please register here] ) to get your [`KType`]([To see links please register here] ). You may want to have a look at the [1.3.40-announcement]([To see links please register here] ) to see whether that might be useful to you.Speaking of the JVM: on the JVM you can also use [`KType.jvmErasure`]( [To see links please register here] ) to get the actual class as also [marstran]([To see links please register here] ) pointed out in the comment. |