0
0
Kotlinprogramming~5 mins

Reified type parameters with inline in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the inline keyword do in Kotlin functions?
The inline keyword tells the compiler to copy the function's code directly where it is called, which can improve performance by avoiding function call overhead.
Click to reveal answer
intermediate
What is a reified type parameter in Kotlin?
A reified type parameter allows you to access the actual type information at runtime inside an inline function, which is normally erased due to type erasure.
Click to reveal answer
intermediate
Why do you need to mark a function as inline to use reified type parameters?
Because only inline functions can keep the type information at runtime by replacing the function call with the actual code, allowing the type to be known and used inside the function.
Click to reveal answer
beginner
How do you declare a function with a reified type parameter?
You declare it with <code>inline</code> and <code>reified</code> keywords like this: <pre>inline fun &lt;reified T&gt; myFunction() { /* use T::class or T::class.java */ }</pre>
Click to reveal answer
intermediate
Give an example use case for reified type parameters.
You can use reified type parameters to check the type at runtime, for example: <pre>inline fun &lt;reified T&gt; isInstance(value: Any) = value is T</pre> This lets you check if a value is of type <code>T</code> without passing the class explicitly.
Click to reveal answer
What keyword must be used with reified type parameters in Kotlin?
Aabstract
Bopen
Cinline
Dsealed
Why can't you normally access generic type information at runtime in Kotlin?
ABecause of type erasure
BBecause Kotlin doesn't support generics
CBecause of inline functions
DBecause of null safety
Which of these is a valid way to get the class of a reified type parameter T inside an inline function?
AclassOf<T>()
BT.class
CT.getClass()
DT::class
What happens if you try to use reified without inline?
ACompilation error
BWorks fine
CRuntime error
DWarning only
Which of these is a benefit of using inline functions with reified type parameters?
ASlower code execution
BAccess to type info at runtime
CMore memory usage
DNo need for generics
Explain what reified type parameters are and why they require inline functions in Kotlin.
Think about how Kotlin normally removes generic types and how inline helps keep them.
You got /4 concepts.
    Describe a simple example where using a reified type parameter makes your Kotlin code easier or cleaner.
    Consider how you might check or get the type inside a generic function.
    You got /4 concepts.