Skip to content
tezvyn:

What is a reified type parameter in Kotlin?

Source: kotlinlang.orgHardHow cards are made

What is a reified type parameter in Kotlin?

Tests your grasp of JVM type erasure and Kotlin's solution. Explain that reified makes a generic type accessible at runtime inside an inline function, avoiding manual Class passing. A red flag is failing to link reified directly to inline functions.

What's really being asked

This question tests your understanding of a core JVM limitation—type erasure—and a specific, advanced Kotlin feature designed to work around it. The interviewer wants to see if you can connect the problem (generic types are erased at runtime) to the mechanism of the solution (inline functions allow the compiler to substitute the actual type at the call site), and finally to the feature itself (reified). It separates candidates who just know the keyword from those who understand the underlying compiler magic.

The full answer

First, define the problem that reified solves: JVM type erasure. Explain that at runtime, a List<String> is just a List, so you cannot perform operations that rely on the generic type, like myVar is T.

Second, describe the traditional workaround, which is to pass the type's Class object as a separate parameter. For example, fun <T> doSomething(item: Any, type: Class<T>). This is functional but verbose at the call site, requiring doSomething(obj, String::class.java).

Third, explain how inline fun <reified T> solves this. The inline keyword causes the compiler to copy the function's bytecode directly into the call site. The reified keyword instructs the compiler to also substitute the generic type T with the actual type used at that call site. This makes the type information available at runtime, allowing for clean, direct type checks like item is T and eliminating the need for a Class parameter.

The mistakes people make

A major red flag is explaining reified without mentioning inline. The reified feature is entirely dependent on inline functions; it cannot be used without it. Stating that reified is a general way to prevent type erasure is incorrect.

Another common mistake is being vague about how it works, simply saying "it keeps the type at runtime." A senior answer must explain the mechanism: the compiler substitutes the function body and the concrete type at the call site. Failing to explain this "how" suggests a surface-level understanding.

What usually comes next

Why can't a reified type parameter be used on a non-inline function? Because without inlining, the function is compiled to a single block of bytecode where the generic type T has already been erased. There is no call site for the compiler to substitute the concrete type into.

What are the performance trade-offs of inline functions? Inlining small functions (especially those with lambdas) can improve performance by removing function call overhead. However, inlining large functions can increase the overall bytecode size, which may negatively impact performance by causing more CPU instruction cache misses.

Can you use reified on a class or a property? No. It's only available for type parameters on inline functions. Type erasure applies to classes and their properties at compile time.

A concrete example

Consider filtering a list of Any for a specific type. The old way is verbose. The reified way is clean.

Without reified: fun <T> filterByType(list: List<Any>, clazz: Class<T>): List<T> = list.filter { clazz.isInstance(it) }.map { it as T }. The call is awkward: filterByType(myList, User::class.java).

With reified: inline fun <reified T> filterByType(list: List<Any>): List<T> = list.filterIsInstance<T>(). The standard library's filterIsInstance uses this exact pattern. The call is elegant: myList.filterIsInstance<User>().

Interview question

What is the core mechanism that allows a `reified` type parameter to be accessible at runtime in a Kotlin `inline` function?

  • a.The compiler copies the function's body and the actual type argument directly into the location where the function is called.Correct
  • b.The `reified` keyword generates special bytecode that the JVM uses to reconstruct the erased type information at runtime.
  • c.The Kotlin runtime maintains a special registry of reified types that can be queried when the function is executed.
  • d.A hidden `Class` object for the generic type is automatically passed as a parameter, making it available inside the function.
Why?

`reified` works because the `inline` function's body and its actual type arguments are copied directly to the call site by the compiler, thus avoiding type erasure. Option D describes a common manual workaround for type erasure, not the automated mechanism of `reified`.

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.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.

See open roles