Sobes.tech
Junior — Middle

How are inline functions with reified types implemented in programming languages, and what happens at compile-time and runtime?

sobes.tech AI

Answer from AI

In Kotlin, inline functions with reified types allow the use of type information at runtime, which is usually impossible due to type erasure.

When declaring a function with the keywords inline and reified, the compiler inlines the function body at the call sites (inline), and also retains information about the type parameter so it can be used inside the function.

Example:

inline fun <reified T> Gson.fromJson(json: String): T {
    return this.fromJson(json, T::class.java)
}

Here, T::class.java is available thanks to the reified type.

At compile time:

  • The function code is copied to the call sites (inline), reducing call overhead.
  • The type T is not erased but becomes accessible inside the function.

At runtime:

  • You can use the type T for reflection or other operations that require type information.
How are inline functions with reified types… - sobes.tech