← Back to Kotlin Course | Chapter 11: Generics | Lesson 4 of 6

Reified Type Parameters

Reified type parameters let an inline function actually check what type it was called with at runtime, something normal generics can't do.

The Type Erasure Problem

On the JVM, generic type information is normally erased at runtime, so a regular generic function cannot check if (value is T) -- the compiler doesn't know what T actually is once compiled.

Example: The Type Erasure Problem

markup
fun <T> describe(value: Any): String {
    return "Checking value: $value"
}

fun main() {
    println(describe<String>("Kotlin"))
}

Reified Solves It with inline

Marking the type parameter reified on an inline function preserves the actual type at each call site, because the function's code (and the real type) get copied directly into the caller.

Example: Reified Solves It with inline

markup
inline fun <reified T> isInstanceOf(value: Any): Boolean = value is T

fun main() {
    println(isInstanceOf<String>("Kotlin"))
    println(isInstanceOf<Int>("Kotlin"))
}

Using T::class with Reified

A reified type parameter also allows accessing T::class directly, which is useful for logging, reflection-lite checks, or building generic factories.

Example: Using T::class with Reified

markup
inline fun <reified T> typeName(): String = T::class.simpleName ?: "Unknown"

fun main() {
    println(typeName<String>())
    println(typeName<Int>())
}

Filtering a List by Reified Type

A common practical use of reified generics is filtering a mixed collection down to only the elements of a specific runtime-checked type.

Example: Filtering a List by Reified Type

markup
inline fun <reified T> filterByType(items: List<Any>): List<T> = items.filterIsInstance<T>()

fun main() {
    val mixed = listOf(1, "two", 3, "four", 5)
    val strings = filterByType<String>(mixed)
    println(strings)
}
Common Mistakes
  1. Trying to use T::class or is T in a regular (non-inline) generic function, which fails to compile because the type is erased at runtime.
  2. Forgetting that reified can only be used on a type parameter of an inline function, since the type information is preserved by inlining, not by the JVM itself.
  3. Overusing reified generics where a simple Class<T> parameter passed explicitly would work just as well and keep the function non-inline.
Chapter Summary
  • Marking a type parameter reified on an inline function lets the actual type be checked and used at runtime inside that function.
  • reified is only allowed on inline functions, because inlining is what preserves the real type information at each call site.
  • Reified type parameters enable operations like is T and T::class that are normally impossible due to JVM type erasure.
  • A common use is a generic helper function that filters or checks a collection by a runtime-provided type.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.