Kotlin reified type parameter can't be used as type parameter in body of function

Viewed 4666

A reified type parameter in Kotlin prevents type parameter erasure and allows the type parameter to be known at run-time. This allows the following code to compile and run as expected:

inline fun <reified T> isA(value: Any) = value is T

However, when I try to use "T" as a type parameter instead of standalone I get a message that it is an erased type. This is demonstrated by the following code that is for illustrative purposes only:

inline fun <reified T> isListOfA(name: String): Boolean {
    val candidate = Class.forName(name)
    return candidate is List<T>
}

Is this due to a technical limitation? If so, what is that limitation?

4 Answers
Related