Sobes.tech
Middle

What do you know about the reified keyword?

sobes.tech AI

Answer from AI

reified is used with inline functions in Kotlin. It preserves type information at runtime, which is usually lost due to type erasure in the JVM. This allows you to treat the type as a regular class, for example, using T::class.java.

inline fun <reified T> printTypeName() {
    // Access to type T at runtime
    println(T::class.java.name)
}

// Usage example
printTypeName<String>() // Outputs java.lang.String

Without reified, you would have to pass Class<T> to the function:

inline fun <T> printTypeName(clazz: Class<T>) {
    // Work with the passed Class<T>
    println(clazz.name)
}

// Usage example
printTypeName(String::class.java)
What do you know about the reified keyword? — Android - sobes.tech