← Back to list

Understanding the reified Keyword in Kotlin: A Deep Dive

When working with generics in Kotlin (or Java), you might have run into a wall when trying to access type information at runtime. This is…

Siddharth · 2025-07-11 17:57 · 4 claps · 2.6 min read paywalled
#reified #kotlin #generics #inline-functions #interview
Open on Medium ↗
Wiki topics: 📱 · Mobile Development

Understanding the reified Keyword in Kotlin: A Deep Dive

When working with generics in Kotlin (or Java), you might have run into a wall when trying to access type information at runtime. This is due to a concept known as type erasure. But Kotlin provides an elegant escape hatch from this limitation through the **reified keyword**—a powerful tool that brings type awareness back into your code, without compromising on generics.

Non members can visit this link

In this blog, we’ll explore:

  • What is type erasure?
  • Why Kotlin introduced reified
  • How to use reified with inline functions
  • Real-world use cases
  • Limitations and best practices

The Problem: Type Erasure on the JVM

Java and Kotlin both run on the JVM, which erases generic type information at runtime. This means the type you pass to a generic class or function is not available during execution.

fun <T> checkType(value: Any): Boolean {
    return value is T // ❌ Compilation error
}

Why the error? Because T doesn't exist at runtime—it’s been "erased". The JVM simply doesn’t know what T was supposed to be.

This becomes especially painful when trying to do operations like:

  • Type checking with is or as
  • JSON parsing with libraries like Gson or Moshi
  • Reflection or class loading

The Kotlin Way: inline + reified

Kotlin offers a clean and intuitive solution:

inline fun <reified T> isOfType(value: Any): Boolean {
    return value is T
}

What’s Happening Here?

This gives you access to the type of T at runtime—which is normally impossible on the JVM!

Practical Examples

1. Type Checking

inline fun <reified T> Any.isOfType(): Boolean = this is T

val result = "Hello".isOfType<String>() // true
val result2 = 123.isOfType<String>()    // false

2. Class Reference

You can easily get the Class<T> instance using:

inline fun <reified T> getTypeClass(): Class<T> = T::class.java

val stringClass = getTypeClass<String>() // java.lang.String

3. Simplified JSON Parsing

When using Gson, this is a typical pain point:

val user = gson.fromJson(json, User::class.java)

But with reified:

inline fun <reified T> Gson.fromJson(json: String): T {
    return this.fromJson(json, T::class.java)
}

val user: User = gson.fromJson(json)

Now you don’t need to pass the Class<T> manually!

How it Works Behind the Scenes

inline fun <reified T> doSomething() {
    println("Called with type: ${T::class}")
}

When you call doSomething<String>(), the compiler replaces the function call with:

println("Called with type: ${String::class}")

This allows the type info to survive beyond compilation into actual runtime logic.

Why You Can’t Use reified Without inline

Because reified types are a compile-time trick. Without inlining, the compiler has no way to “inject” the real type of T into the function body—because it would be erased by the JVM otherwise.

That’s why this won’t compile:

fun <reified T> myFunction() {} // ❌ Error: reified type parameters are only allowed on inline functions

Limitations

  1. reified only works with inline functions.
  2. You can’t create instances of T directly (T()), unless you pass a constructor or use reflection.
  3. Overuse of inline can increase bytecode size due to function duplication.

Best Practices

  • Use reified when you need the type at runtime, such as for type checks, reflection, or JSON deserialization.
  • Avoid using inline just to use reified—only apply it when necessary.
  • Keep inline functions small and focused to avoid code bloat.

Final Thoughts

Kotlin’s reified keyword is one of those hidden gems that makes the language more powerful and expressive than Java. It elegantly bridges the gap between generic programming and runtime type introspection, a feat that's notoriously tricky in other JVM languages.

Whether you’re building a type-safe API, a DSL, or simplifying your JSON parsing logic, understanding reified is a must-have tool in your Kotlin toolbox.


메타데이터
post_id
0dfc98aa4658
slug
understanding-the-reified-keyword-in-kotlin-a-deep-dive-0dfc98aa4658
url
https://medium.com/@sidcasm/understanding-the-reified-keyword-in-kotlin-a-deep-dive-0dfc98aa4658
canonical_url
https://medium.com/@sidcasm/understanding-the-reified-keyword-in-kotlin-a-deep-dive-0dfc98aa4658
author_url
https://medium.com/@sidcasm
status
ok
fetched_at
2026-07-18 11:33:39