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 , 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") // trueAll lessons in this course
- inline Functions: Eliminating Lambda Overhead
- noinline and crossinline Modifiers
- reified Type Parameters: Accessing T at Runtime
- Practical Reified Patterns: Parsing, DI & Serialization