samedi 28 novembre 2015

Kotlin: Reflection exposes KClassImpl to me rather than proper type

It seems that Kotlin is exposing a KClassImpl to me rather than the real type.

For example:

val TYPE_TO_BYTES = mapOf(Boolean::class to 1, Byte::class to 1, Short::class to 2)

inline fun <reified T> read(address: Long): T {
    val type = T::class
    val bytes = TYPE_TO_BYTES.getRaw(type) // null
    ...
}

Yet when I call methods on T::class they seem correct; for example T::class.simpleName does give me the proper values.

If I print type it shows kotlin.reflect.jvm.internal.KClassImpl, thus the reason TYPE_TO_BYTES doesn't work correctly.

I "patched" this by both mapping and getting using qualifiedName but this isn't as elegant and isn't as safe as possible for my use case.





Aucun commentaire:

Enregistrer un commentaire