0
0
Kotlinprogramming~10 mins

Reified type parameters with inline in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reified type parameters with inline
Call inline function with reified type T
Compiler keeps type info of T
Use type T inside function (e.g., is, class check)
Function executes with real type info
Return or print result based on T
The inline function keeps the actual type T at runtime, allowing type checks and casts inside the function.
Execution Sample
Kotlin
inline fun <reified T> isString(value: Any): Boolean {
    return value is T
}

fun main() {
    println(isString<String>("hello"))
    println(isString<Int>(123))
    println(isString<String>(123))
}
This code checks if a value is of type T using a reified type parameter inside an inline function.
Execution Table
StepFunction CallT TypeValueCheck (value is T)ResultOutput
1isString<String>("hello")String"hello""hello" is Stringtruetrue
2isString<Int>(123)Int123123 is Inttruetrue
3isString<String>(123)String123123 is Stringfalsefalse
4End of main----Program ends
💡 All calls complete; program ends after printing results.
Variable Tracker
VariableStartCall 1Call 2Call 3Final
T-StringIntString-
value-"hello"123123-
Result-truetruefalse-
Key Moments - 2 Insights
Why can we use 'value is T' inside the function?
Because the function is inline with a reified type parameter, the compiler keeps the actual type T at runtime, allowing 'is' checks (see execution_table rows 1-3).
What happens if the function was not inline and T was not reified?
The type T would be erased at runtime, so 'value is T' would cause a compile error or not work, unlike the inline reified case shown in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when calling isString<String>("hello")?
Atrue
Bfalse
CCompile error
Dnull
💡 Hint
Check execution_table row 1 under Output column.
At which step does the check 'value is T' return false?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at execution_table row 3 under Check and Result columns.
If the function was not inline, what would happen to the 'value is T' check?
AIt would work the same
BIt would always return true
CIt would cause a compile error
DIt would always return false
💡 Hint
Refer to key_moments explanation about non-inline functions.
Concept Snapshot
inline fun <reified T> allows using T's type at runtime
Use 'value is T' inside such functions
Compiler keeps T info due to inline
Enables safe type checks and casts
Without inline, T is erased at runtime
Full Transcript
This example shows how Kotlin's inline functions with reified type parameters keep the actual type information at runtime. The function isString uses 'value is T' to check if the value matches the type T. Because the function is inline and T is reified, the compiler replaces T with the real type during compilation. The execution table shows calls with different types and values, and the results of the type checks. Key moments clarify why reified is needed and what happens without it. The visual quiz tests understanding of outputs and behavior changes if inline is removed.