In Kotlin, I can obtain a KType from a KClass<*> like so:

Int::class.createType()

Kotlin .智力

How do I do the reverse and obtain the KClass<Int> from a KType representing a Kotlin .智力?

推荐答案

You can use KType.classifier for this:

val intType : KType = Int::class.createType()
val intClassifier : KClassifier? = intType.classifier
assertEquals(Int::class, intClassifier) // true

请注意,由于1.3.40,您还可以(至少在JVM上)使用实验性的typeOf<Int>()来获得KType.你可能想看看1.3.40-announcement,看看它是否对你有用.

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.

Kotlin相关问答推荐

如何在使用Kotlin Coroutines时检测和记录何时出现背压

Kotlin中的增广赋值语句中的难以理解的错误

如何为集成测试配置Gradle JVM测试套件?

在Kotlin中将String转换为T

使用另一个对象的列表创建对象

ActivityResultContracts TakePicture 结果总是返回 false

使用事务时未调用 Kafka ConsumerInterceptor onCommit

如何在 Kotlin 中将with代码转换为完整代码?

测试协程和线程之间的差异,我在kotlin中使用线程时无法得到OOM错误

如何从 Firestore 查询中排除元素?

在 Kotlin 中实现 (/inherit/~extend) 注解

Kotlin内联扩展属性

Kotlin not nullable值可以为null吗?

如何在kotlin语言中将字符转换为ascii值

用Gradle Kotlin DSL构建源jar?

Kotlin 中更好的回调方法是什么?侦听器与高阶函数

Gradle:无法连接到 Windows 上的 Kotlin 守护程序

Kotlin数据类打包

如何在Kotlin中将字符串转换为InputStream?

如何在 Gradle Kotlin DSL 中使用来自 gradle.properties 的插件版本?