2

I use reified generics to capture KClass<>

inline fun <reified T> register(generator: Generator<T>) {
    register(T::class, generator)
}

But KType has some additional properties which are interesting to me like nullability. I would like to write something like this to capture the KType instead.

inline fun <reified T> register(generator: Generator<T>) {
    register(T::ktype, generator) // Doesn't compile
}

...

register<String?>(myGen) // The nullability of String? is captured

Is there someway to capture KType instead of KClass<>?

Thanks in advance.

1 Answers1

3

No, as for now, this cannot be done, there is an issue in the Kotlin issue tracker about it, currently marked as To be discussed.

You can, however, get more than just KClass<T> from the reified type parameter using subclassing and, in particular, the technique called super type tokens.

Community
  • 1
  • 1
hotkey
  • 111,884
  • 27
  • 298
  • 285