mercredi 2 mai 2018

Get runtime class of a generic type parameter in Kotlin without reified keyword

Kotlin provides a way to get runtime class of a generic type parameter with reified keyword which can only be used with inline functions. For example the following works:

inline fun <reified T> test() : T {
  println(T::class.java)
}

However, I need a way of doing this in a non-inline function. Since the reified keyword is not an option for non-inline functions, the following does not work:

fun <reified T> test() : T {
  println(T::class.java)
}

Does Kotlin have a different method to solve this problem? I know taking the Class<T> as a parameter can be a solution but I don't want to pass an extra argument to my function for this purpose.





Aucun commentaire:

Enregistrer un commentaire