Reified Type Parameters

Type Erasure

On the JVM, generic types are erased at runtime: inside fun <T> foo() you cannot write x is T or T::class. With inline fun <reified T> the real type is copied into each call site, so you can.

Example

inline fun <reified T> List<Any>.filterType(): List<T> = filter { it is T }.map { it as T }

inline fun <reified T> typeName(): String = T::class.simpleName ?: "?"

inline fun <reified T : Enum<T>> parseEnum(value: String, default: T): T =
    enumValues<T>().firstOrNull { it.name.equals(value, ignoreCase = true) } ?: default

enum class PaymentMode { UPI, CARD, CASH }

fun main() {
    val mixed: List<Any> = listOf("Asha", 42, "Vikram", 3.5, 7)
    println("Strings: ${mixed.filterType<String>()}")
    println("Ints:    ${mixed.filterType<Int>()}")
    println("Type name: ${typeName<Map<String, Int>>()}")
    println(parseEnum("upi", PaymentMode.CASH))
    println(parseEnum("crypto", PaymentMode.CASH))
}

Output

Strings: [Asha, Vikram]
Ints:    [42, 7]
Type name: Map
UPI
CASH

Reified in Real APIs

// Android: start an Activity without ::class.java
inline fun <reified A : Activity> Context.startActivity(configure: Intent.() -> Unit = {}) {
    startActivity(Intent(this, A::class.java).apply(configure))
}
startActivity<OrderDetailActivity> { putExtra("orderId", 42L) }

// Gson / kotlinx.serialization
inline fun <reified T> Json.decode(text: String): T = decodeFromString<T>(text)

// Koin / Hilt-style lookups
val repo: ProductRepository = get<ProductRepository>()

Note: reified works only on inline functions, and the type must be known at the call site (you cannot pass a non-reified T into it).