0Pricing
Kotlin Academy · Lesson

reified Type Parameters: Accessing T at Runtime

Use reified with inline functions to perform type checks and casts at runtime.

reified Type Parameters: Accessing T at Runtime is a free Kotlin Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Type Erasure Problem

Kotlin/JVM erases generic type information at runtime. Inside a generic function fun foo(), you cannot write T::class or is T — the JVM has no idea what T is after compilation.

reified to the Rescue

Combine inline with reified on the type parameter. Because the function is inlined, the compiler substitutes the actual type at every call site — making T fully accessible at runtime.

inline fun <reified T> isInstance(value: Any): Boolean = value is T

val result = isInstance<String>("hello")  // true

Checking Type with is T

Without reified, is T causes a compile-time error ("Cannot check for instance of erased type"). With it, the compiler rewrites the check using the concrete type provided at the call site.

inline fun <reified T> filterByType(list: List<Any>): List<T> =
    list.filterIsInstance<T>()

Getting KClass from T

You can obtain the KClass of the type parameter with T::class. This enables reflection-free type checks and class references passed to APIs.

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

println(className<Int>())  // "Int"

Parsing with reified

A common pattern is a type-safe parseJson helper that receives the target class automatically:

inline fun <reified T> parseJson(json: String): T =
    jacksonObjectMapper().readValue(json, T::class.java)

ViewModel / Service Locator Pattern

Dependency injection containers often use reified to return the correct instance without requiring you to pass a Class argument:

inline fun <reified T : ViewModel> Fragment.viewModel(): T =
    ViewModelProvider(this)[T::class.java]

Calling Java APIs with Class<T>

Many Java APIs require a Class argument. reified lets you call them without forcing callers to pass the class explicitly:

inline fun <reified T> Gson.fromJson(json: String): T =
    fromJson(json, T::class.java)

Reified and Type Constraints

You can combine reified with upper bounds:

inline fun <reified T : Any> create(): T =
    T::class.java.getDeclaredConstructor().newInstance()

Limitations

Reified type parameters work only in inline functions — not in classes, interfaces, or non-inline functions. You cannot call a reified function with an unknown type known only at runtime.

How the Compiler Implements It

At each call site, the compiler replaces T with the actual type argument. The bytecode at the call site contains direct references to the concrete class — no reflection needed.

Standard Library Examples

Standard library functions using reified: filterIsInstance(), emptyArray(), arrayOf(), typeOf(). These work precisely because they are inline + reified.

Quick Check

Why does reified require the function to be inline?

Recap: reified Type Parameters

Key takeaways:

  • reified makes a type parameter accessible at runtime, bypassing type erasure
  • Requires the function to be inline
  • Enables is T, T::class, T::class.java inside the function
  • Powers type-safe JSON parsing, DI helpers, and class-reference APIs

Frequently asked questions

Is the “reified Type Parameters: Accessing T at Runtime” lesson free?

Yes — the full text of “reified Type Parameters: Accessing T at Runtime” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “reified Type Parameters: Accessing T at Runtime”?

Use reified with inline functions to perform type checks and casts at runtime. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “reified Type Parameters: Accessing T at Runtime” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. inline Functions: Eliminating Lambda Overhead
  2. noinline and crossinline Modifiers
  3. reified Type Parameters: Accessing T at Runtime
  4. Practical Reified Patterns: Parsing, DI & Serialization
← Back to Kotlin Academy