Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A reified type parameter is a Kotlin language feature that preserves generic type information at runtime, bypassing the JVM’s default type erasure. By marking a type parameter with the reified modifier within an inline function or an inline extension property, the compiler embeds the actual type argument directly into the bytecode at the call site, enabling runtime operations like type checking (is T), casting (as T), and class referencing (T::class.java).

The Mechanism of Type Erasure

By default, the JVM erases generic type parameters during compilation. In a standard generic function, the type T is treated as Any? (or its upper bound) at runtime, making direct type evaluation impossible.
// This will not compile: "Cannot check for instance of erased type: T"
fun <T> checkType(item: Any): Boolean {
    return item is T 
}

// This will not compile: "Cannot use 'T' as reified type parameter"
fun <T> getType(): KClass<*> {
    return T::class
}

The reified Syntax

Kotlin resolves type erasure by combining the inline modifier with the reified type parameter modifier.
inline fun <reified T> checkType(item: Any): Boolean {
    return item is T // Compiles successfully
}

// Compiles successfully and works with nullable types.
// T::class.java returns Class<T & Any> because Java Class objects cannot represent nullability.
inline fun <reified T> getJavaClass(): Class<T & Any> {
    return T::class.java 
}

How It Works Under the Hood

The reified modifier is strictly dependent on the inline keyword. When a function or property getter is marked as inline, the Kotlin compiler does not invoke it via standard method dispatch. Instead, it copies the bytecode directly into the call site. Because the compiler knows the exact type argument provided at the specific call site during compilation, it replaces the generic type parameter T with the actual compiled class reference during the inline expansion. Kotlin Call Site:
val isString = checkType<String>(123)
Decompiled Java/Bytecode Equivalent:
// The compiler replaces 'T' with 'String' directly in the generated bytecode
Object item = 123;
boolean isString = item instanceof String;

Technical Constraints and Rules

  • Inline Requirement: The reified modifier can only be applied to type parameters of inline functions or inline extension properties (specifically, extension properties with inline getters). It cannot be used on standard functions, classes, or interfaces.
// Valid use of reified on an inline extension property
inline val <reified T> T.className: String 
    get() = T::class.java.name
  • Java Interoperability: Functions with reified type parameters cannot be called from Java code. Java does not support Kotlin’s inline mechanics and therefore cannot perform the necessary bytecode substitution at the call site.
  • Instantiation: While reified preserves the type reference, it does not allow calling a constructor of T (e.g., return T()). The compiler holds the Class reference but does not inherently know if a specific constructor exists.
  • Platform Declaration Clash: Like all generic type parameters on the JVM, reified type parameters are subject to signature clashes. The JVM type erasure constraint applies to the method signature itself, meaning functions cannot be overloaded if their signatures erase to the same JVM signature. Overloading functions based solely on different generic upper bounds will result in a compilation error.
// Compilation error: Platform declaration clash
// Both functions erase to the exact same JVM signature: `foo()`
inline fun <reified T : String> foo() {}
inline fun <reified T : Int> foo() {}
  • Reflection: Reified types allow direct access to Kotlin reflection (T::class) and Java reflection (T::class.java) without requiring a Class<T> parameter to be passed manually into the function signature.
Master Kotlin with Deep Grasping Methodology!Learn More