0Pricing
Kotlin Academy · Lesson

reified Type Parameters: Accessing T at Runtime

Use reified with inline functions to perform type checks and casts at runtime.

The Type Erasure Problem

Kotlin/JVM erases generic type information at runtime. Inside a generic function fun foo(), you cannot write T::class or is T — the JVM has no idea what T is after compilation.

reified to the Rescue

Combine inline with reified on the type parameter. Because the function is inlined, the compiler substitutes the actual type at every call site — making T fully accessible at runtime.

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

val result = isInstance<String>("hello")  // true

All lessons in this course

  1. inline Functions: Eliminating Lambda Overhead
  2. noinline and crossinline Modifiers
  3. reified Type Parameters: Accessing T at Runtime
  4. Practical Reified Patterns: Parsing, DI & Serialization
← Back to Kotlin Academy