Blog

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

© 2025 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>()

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
Java
What is type erasure in Java generics and what limitation does it cause?
#java#generics#type-erasure