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 <reified T> 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 <reified T> 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?✗ Incorrect
Reified type parameters require the function to be marked as
inline to preserve type information at runtime.Why can't you normally access generic type information at runtime in Kotlin?
✗ Incorrect
Kotlin uses type erasure on generics, which removes type information at runtime.
Which of these is a valid way to get the class of a reified type parameter
T inside an inline function?✗ Incorrect
Inside an inline function with reified
T, you can use T::class to get the Kotlin class reference.What happens if you try to use
reified without inline?✗ Incorrect
Using
reified without inline causes a compilation error because the type information cannot be preserved.Which of these is a benefit of using
inline functions with reified type parameters?✗ Incorrect
Inlining with reified type parameters allows access to the actual type at runtime, which is normally erased.
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.