Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Kotlin
Kotlinhard

What are reified generics and why do they require `inline`?

Tags
#reified#inline#generics
Back to categoryPractice quiz

Answer

Because of type erasure, generic type info isn’t available at runtime. `reified` in an `inline` function keeps the type at the call site, so you can do things like `T::class` or `value is T` safely.

inline fun <reified T> Any?.asTypeOrNull(): T? = this as? T

val x: Any = 123
val n: Int? = x.asTypeOrNull<Int>()

Advanced answer

Deep dive

On the JVM, generics are erased at runtime (you can’t normally do `value is T`).

`reified` + `inline` works because the compiler substitutes the function body at the call site, so it can embed the concrete type info.

Example

inline fun <reified T> Any?.asOrNull(): T? = this as? T

val x: Any = 1
val i: Int? = x.asOrNull<Int>()

Common pitfalls

  • Thinking reified removes type erasure globally (it’s only for inline call sites).
  • Using it where you still need full generic info (e.g., `List<T>` element type at runtime is still tricky without type tokens).

Related questions

Kotlin
Inline lambdas and non-local returns: what are `crossinline` and `noinline` for?
#kotlin#inline#crossinline
Kotlin
Generics variance: what do `out` and `in` mean in Kotlin?
#kotlin#generics#variance
Kotlin
What does `inline` do and when can it help performance?
#inline#performance
#lambdas
TypeScript
What are generics in TypeScript and why use them?
#generics#types
Java
What is type erasure in Java generics and what limitation does it cause?
#java#generics#type-erasure