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 theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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 typeT is treated as Any? (or its upper bound) at runtime, making direct type evaluation impossible.
The reified Syntax
Kotlin resolves type erasure by combining the inline modifier with the reified type parameter modifier.
How It Works Under the Hood
Thereified 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:
Technical Constraints and Rules
- Inline Requirement: The
reifiedmodifier can only be applied to type parameters ofinlinefunctions or inline extension properties (specifically, extension properties with inline getters). It cannot be used on standard functions, classes, or interfaces.
- Java Interoperability: Functions with
reifiedtype parameters cannot be called from Java code. Java does not support Kotlin’sinlinemechanics and therefore cannot perform the necessary bytecode substitution at the call site. - Instantiation: While
reifiedpreserves the type reference, it does not allow calling a constructor ofT(e.g.,return T()). The compiler holds theClassreference 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.
- Reflection: Reified types allow direct access to Kotlin reflection (
T::class) and Java reflection (T::class.java) without requiring aClass<T>parameter to be passed manually into the function signature.
Master Kotlin with Deep Grasping Methodology!Learn More





