> ## 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.

# Kotlin Reified Type Parameter

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.

```kotlin theme={"dark"}
// 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.

```kotlin theme={"dark"}
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:**

```kotlin theme={"dark"}
val isString = checkType<String>(123)
```

**Decompiled Java/Bytecode Equivalent:**

```java theme={"dark"}
// 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.

```kotlin theme={"dark"}
// 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.

```kotlin theme={"dark"}
// 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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
