← Back to list

How to Use “reified” in Kotlin to Access Generic Type Information

When working with generics in Kotlin, you may have encountered type erasure, where type information is lost at runtime. Kotlin provides a…

amol pawar in softAai Blogs · 2025-03-18 05:30 · 51 claps · 2.5 min read
#reified-type #reified #kotlin #kotlin-beginners #android
Open on Medium ↗
Wiki topics: 📱 · Mobile Development

How to Use “reified” in Kotlin to Access Generic Type Information

When working with generics in Kotlin, you may have encountered type erasure, where type information is lost at runtime. Kotlin provides a powerful feature called the **“reified” type** to overcome this limitation, allowing us to access generic type information at runtime.

In this article, we’ll explore how reified types work, why they are useful, and how to implement them effectively in Kotlin.

Why Do We Need “Reified” in Kotlin?

When working with generics, Kotlin (like Java) erases type parameters at runtime. This means that when we pass a generic type, the actual type information isn’t available during execution.

fun <T> getTypeInfo(): Class<T> {
    return T::class.java // Error: Cannot use 'T' as reified type parameter
}

This code won’t compile because T::class.java requires a reified type parameter, which isn’t available by default in regular generic functions.

Solution: Using “Reified” Type with Inline Functions

To preserve generic type information at runtime, we need to mark the function as inline and use the reified keyword:

inline fun <reified T> getTypeInfo(): Class<T> {
    return T::class.java
}

fun main() {
    val type = getTypeInfo<String>()
    println(type) // Output: class java.lang.String
}

Now, T is reified, meaning its type information is retained at runtime, and we can access it without reflection hacks.

Understanding “Reified” Type in Kotlin

When we use reified types, the compiler replaces the generic type T with the actual type parameter used in the function call. This is possible because the function is marked inline, meaning its bytecode is directly inserted at call sites.

Practical Examples of “Reified” Type

Checking an Object’s Type

Instead of using is checks manually, we can simplify the process:

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

fun main() {
    println(isTypeMatch<String>("Hello")) // true
    println(isTypeMatch<Int>("Hello"))    // false
}

Creating Instances of Generic Classes

We can use reified types to instantiate objects dynamically:

inline fun <reified T> createInstance(): T? {
    return T::class.java.getDeclaredConstructor().newInstance()
}

class ExampleClass {
    fun show() = println("Instance Created")
}

fun main() {
    val instance = createInstance<ExampleClass>()
    instance?.show() // Output: Instance Created
}

Filtering a List Based on Type

Using reified, we can filter elements of a list dynamically:

inline fun <reified T> List<Any>.filterByType(): List<T> {
    return this.filterIsInstance<T>()
}

fun main() {
    val items = listOf(1, "Hello", 2.5, "Kotlin", 42)
    val strings: List<String> = items.filterByType()
    println(strings) // Output: [Hello, Kotlin]
}

Why “reified” Works Only with Inline Functions

The reified type only works with inline functions because the compiler replaces the generic type T with the actual type at compile time. This prevents type erasure and allows us to access type information.

If we remove inline, the code won't compile because T would be erased at runtime.

Limitations of “Reified” Type

While reified types are useful, there are some restrictions:

  1. Only Works in Inline Functionsreified cannot be used in normal functions.
  2. Cannot Be Used in Class-Level Generics — It’s specific to functions and doesn’t work with class type parameters.
  3. Code Size Increase Due to Inlining — Since the function is inlined, it may increase the bytecode size if used excessively.

Dive deeper into this topic here: [Main Article URL]

Conclusion

The “reified” type keyword in Kotlin is a game-changer for handling generics efficiently. It allows us to retain type information at runtime, eliminating the need for reflection while improving performance and readability. Whether you’re checking types, filtering lists, or dynamically creating instances, reified types make generic programming in Kotlin much more powerful and intuitive.

So, next time you work with generics, try leveraging reified types and experience the difference..!


메타데이터
post_id
8ccc2fc91e38
slug
how-to-use-reified-in-kotlin-to-access-generic-type-information-8ccc2fc91e38
url
https://medium.com/softaai-blogs/how-to-use-reified-in-kotlin-to-access-generic-type-information-8ccc2fc91e38
canonical_url
https://medium.com/softaai-blogs/how-to-use-reified-in-kotlin-to-access-generic-type-information-8ccc2fc91e38
author_url
https://medium.com/@amoljp19
status
ok
fetched_at
2026-07-18 11:33:39