Kotlin Academy · Lesson

reified Type Parameters: Accessing T at Runtime

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

Lesson 3 of 413 steps

reified Type Parameters: Accessing T at Runtime is a free Kotlin Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.

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
Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

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

Yes — any 3 lessons of the Kotlin Academy learning path, including “reified Type Parameters: Accessing T at Runtime”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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