← Back to Kotlin Course | Chapter 11: Generics | Lesson 4 of 6

Reified Type Parameters

Reified type parameters let an inline function actually check what type it was called with at runtime, something normal generics can't do.
Syntax
kotlin
inline fun <reified T> functionName(value: Any): Boolean {
    return value is T            // T is available at runtime
}

inline fun <reified T> typeName(): String? = T::class.simpleName

The Type Erasure Problem

On the JVM, generic type information is normally erased at runtime, so a regular generic function cannot check if (value is T) -- the compiler doesn't know what T actually is once compiled.

Example: The Type Erasure Problem

markup
fun <T> describe(value: Any): String {
    // Return `"Checking value: $value"` from this function
    return "Checking value: $value"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `describe<String>("Kotlin")` to the console, with a trailing newline
    println(describe<String>("Kotlin"))
}

Reified Solves It with inline

Marking the type parameter reified on an inline function preserves the actual type at each call site, because the function's code (and the real type) get copied directly into the caller.

Example: Reified Solves It with inline

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

// Entry point: execution of the program starts here
fun main() {
    // Print `isInstanceOf<String>("Kotlin")` to the console, with a trailing newline
    println(isInstanceOf<String>("Kotlin"))
    // Print `isInstanceOf<Int>("Kotlin")` to the console, with a trailing newline
    println(isInstanceOf<Int>("Kotlin"))
}

Using T::class with Reified

A reified type parameter also allows accessing T::class directly, which is useful for logging, reflection-lite checks, or building generic factories.

Example: Using T::class with Reified

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

// Entry point: execution of the program starts here
fun main() {
    // Print `typeName<String>()` to the console, with a trailing newline
    println(typeName<String>())
    // Print `typeName<Int>()` to the console, with a trailing newline
    println(typeName<Int>())
}

Filtering a List by Reified Type

A common practical use of reified generics is filtering a mixed collection down to only the elements of a specific runtime-checked type.

Example: Filtering a List by Reified Type

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

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `mixed`, set to `listOf(1, "two", 3, "four", 5)`
    val mixed = listOf(1, "two", 3, "four", 5)
    // Declare a read-only variable `strings`, set to `filterByType<String>(mixed)`
    val strings = filterByType<String>(mixed)
    // Print `strings` to the console, with a trailing newline
    println(strings)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to use T::class or is T in a regular (non-inline) generic function, which fails to compile because the type is erased at runtime.
  2. Forgetting that reified can only be used on a type parameter of an inline function, since the type information is preserved by inlining, not by the JVM itself.
  3. Overusing reified generics where a simple Class<T> parameter passed explicitly would work just as well and keep the function non-inline.
Chapter Summary
  • Marking a type parameter reified on an inline function lets the actual type be checked and used at runtime inside that function.
  • reified is only allowed on inline functions, because inlining is what preserves the real type information at each call site.
  • Reified type parameters enable operations like is T and T::class that are normally impossible due to JVM type erasure.
  • A common use is a generic helper function that filters or checks a collection by a runtime-provided type.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.