What is a reified type parameter in Kotlin?

Tests JVM type erasure and inline function mechanics. Strong answer: reified preserves generic types at runtime via inline expansion, enabling is T checks without Class<T> passing. Red flag: claiming reified works without inline or outside functions.
WHAT THIS TESTS: This tests whether you understand JVM type erasure and how Kotlin's inline machinery bypasses it at compile time. Senior engineers should know why you cannot normally write is T or reference T::class inside a generic function, and how reified changes the bytecode generation story so the concrete type survives at the call site.
A GOOD ANSWER COVERS: First, explain type erasure: on the JVM, generic type parameters like T are erased to Object or their bound at runtime, so foo<T>() has no access to T after compilation. Second, explain inline functions: the compiler copies the function body into every call site, which means the concrete type argument is known during that inlining pass. Third, explain the reified keyword: marking a type parameter as reified tells the compiler to substitute the actual type into the inlined body, making T available at runtime in that specific call site. Fourth, give the practical payoff: you can write is T, T::class, and type checks without passing Class<T> objects or using reflection.
COMMON WRONG ANSWERS: A red flag is claiming reified works on regular non-inline functions or inside classes as members. Another is saying it is just compiler sugar for passing MyClass::class.java automatically; while similar in effect, the mechanism is bytecode substitution at the call site, not reflection. Also, watch out for saying reified eliminates type erasure globally; it only works per call site of an inline function.
LIKELY FOLLOW-UPS: An interviewer might ask about the cost: inlining increases bytecode size, so reified utilities should stay small and focused. They might ask where reified fails: it cannot be used with class members, only top-level or local functions, because the JVM cannot inline across virtual dispatch. They might also ask how this compares to Java's Class<T> pattern or to using instanceof directly.
ONE CONCRETE EXAMPLE: The canonical example from the documentation is a tree traversal where you want findParentOfType<MyNodeType>(). Without reified, you must declare fun <T> findParentOfType(clazz: Class<T>) and call it with MyNodeType::class.java, then use clazz.isInstance inside a loop. With reified, you write inline fun <reified T> findParentOfType(): T? and use p !is T directly at the call site. The compiler inlines the function and replaces T with MyNodeType, emitting a real instanceof check in the generated bytecode.
Source: kotlinlang.org
Read the original → kotlinlang.org
- #kotlin
- #jvm
- #generics
- #inline-functions
- #type-erasure
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.