Because of type erasure, generic type info isn’t available at runtime. `reified` in an `inline` function keeps the type at the call site, so you can do things like `T::class` or `value is T` safely.
inline fun <reified T> Any?.asTypeOrNull(): T? = this as? T
val x: Any = 123
val n: Int? = x.asTypeOrNull<Int>()On the JVM, generics are erased at runtime (you can’t normally do `value is T`).
`reified` + `inline` works because the compiler substitutes the function body at the call site, so it can embed the concrete type info.
inline fun <reified T> Any?.asOrNull(): T? = this as? T
val x: Any = 1
val i: Int? = x.asOrNull<Int>()