Sobes.tech
Junior — Middle

What is the purpose of the reified modifier in Kotlin?

sobes.tech AI

Answer from AI

In Kotlin, the reified modifier is used with inline functions to preserve type information at runtime. Usually, due to type erasure in the JVM, information about generic types is not available at runtime, but with reified, you can access the type T inside the function.

This allows, for example, type checks or creating instances of classes using T::class or T::class.java inside the function.

Example:

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

val result = isInstance<String>("test") // true

Without reified, such code is impossible because the type T is not available at runtime.