Skip to main content
InterviewHubPRO
Section Overview
Q43

Question 43 of 50

Part 3: K2 Compiler, KMP, Advanced Metaprogramming & Optimization (25 - 35)

86%
Kotlin & Coroutines (50 Qs)•Part 3: K2 Compiler, KMP, Advanced Metaprogramming & Optimization (25 - 35)

Reified Type Parameters (`inline fun <reified T>`): Preserving generic types at runtime.

senior LevelCode ExamplePerformanceTypeScript

In Simple Words: On the JVM, generics undergo Type Erasure (e.g. List<String> becomes raw List at runtime; T::class.java fails to compile):

  • reified Modifier: Combined with inline, the compiler replaces occurrences of T with the concrete class type directly at the call site:
    kotlin
    inline fun <reified T> Gson.fromJson(json: String): T {
      return fromJson(json, T::class.java)
    }
    val user: User = gson.fromJson(jsonString)
    
  • Eliminates the need to pass explicit Class<T> arguments!