Reified Type Parameters: Accessing Generic Types at Runtime

Reified type parameters let you access generic types at runtime, bypassing JVM type erasure. In Kotlin, you use this inside inline functions to check types (obj is T) or get a class reference. The footgun: reified requires inline to work.
Why it exists
On the JVM, type information for generics is normally erased at compile time. A List<String> becomes just a List at runtime, so you can't check an object against a generic type. This forces developers into clumsy workarounds, like passing String::class.java as an extra parameter to a function.
The mental model
Think of reified as making a generic type parameter "real" inside a function. It's a compile-time instruction that says, "Instead of erasing this type, find the actual type used at the call site and substitute it directly into the function's bytecode." The type is no longer just a placeholder; it's a concrete class you can work with.
How it works
Reified parameters are only possible on inline functions. When you call an inline function, the compiler copies its body directly to the call site. If that function has a reified type parameter, the compiler also replaces the generic T with the actual type argument. A call like myFunc<User>() results in compiled code where T is literally User. This allows you to perform runtime checks like obj is T or access the type's class with T::class.java.
When to use it
Use reified when a generic function needs to know about its type argument. This is perfect for building clean APIs. Examples include: deserializing JSON (moshi.adapter<User>().fromJson(jsonString)), finding views in Android, or creating type-safe database queries. It eliminates the need to pass class objects as arguments.
When not to use it
Do not use reified on non-inline functions, as it will cause a compile-time error. You also can't use it for properties or class type parameters. Be mindful that inlining, especially with large functions, can increase the size of the generated code, so use it where the API clarity and type safety benefits are clear.
One canonical example
Imagine finding a parent node of a specific type in a tree. Without reified types, the function is clunky: fun <T> findParentOfType(clazz: Class<T>): T?. The call looks like this: node.findParentOfType(MyNode::class.java). With a reified type, the function becomes inline fun <reified T> findParentOfType(): T?. The call is much cleaner and more intuitive: node.findParentOfType<MyNode>().
Interview question
What is the fundamental reason Kotlin's reified type parameters are exclusively usable with inline functions?
- a.Inline functions are required to access the private members of the generic type T at runtime.
- b.Inline functions ensure that the generic type's Class object is implicitly passed as a hidden argument.
- c.The compiler uses inline functions to substitute the actual type argument directly into the function's bytecode at the call site.Correct
- d.Reified types introduce runtime performance penalties that only inline functions can mitigate through optimization.
Why? this is the answer
The card states that when an inline function is called, the compiler copies its body to the call site and replaces the reified generic type with the actual type argument. This compile-time substitution is the core mechanism that inline functions enable for reified types. Option B describes a workaround that reified types aim to eliminate, not how they function with inline.
Just read this? Test yourself on what you have been reading.
Read the original → kotlinlang.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.
See open roles