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?
Aabstract
Bopen
Cinline
Dsealed
✗ 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?
ABecause of type erasure
BBecause Kotlin doesn't support generics
CBecause of inline functions
DBecause of null safety
✗ 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?
AclassOf<T>()
BT.class
CT.getClass()
DT::class
✗ 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?
ACompilation error
BWorks fine
CRuntime error
DWarning only
✗ 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?
ASlower code execution
BAccess to type info at runtime
CMore memory usage
DNo need for generics
✗ 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.
Practice
(1/5)
1. What is the main benefit of using reified type parameters in Kotlin inline functions?
easy
A. They improve performance by avoiding inline expansion.
B. They allow access to the actual type at runtime inside the function.
C. They enable functions to be called without specifying type arguments.
D. They automatically convert types to strings.
Solution
Step 1: Understand reified type parameters
Reified type parameters let you use the real type inside an inline function at runtime, unlike normal generics which erase type info.
Step 2: Identify the main benefit
This means you can do type checks and casts safely inside the function using the actual type.
Final Answer:
They allow access to the actual type at runtime inside the function. -> Option B
Quick Check:
Reified = real type access [OK]
Hint: Reified means real type info available at runtime [OK]
Common Mistakes:
Thinking reified improves performance by avoiding inline
Believing reified removes need for type arguments
Confusing reified with automatic type conversion
2. Which of the following is the correct syntax to declare an inline function with a reified type parameter T in Kotlin?
easy
A. inline fun myFunction() { }
B. inline fun <T> myFunction() { }
C. fun inline myFunction() { }
D. fun inline myFunction() { }
Solution
Step 1: Recall syntax for inline with reified
The correct syntax places inline before the function and reified before the type parameter inside angle brackets.
Step 2: Check each option
inline fun myFunction() { } correctly uses inline fun <reified T> myFunction(). Others have wrong order or missing keywords.
Final Answer:
inline fun <reified T> myFunction() { } -> Option A
Quick Check:
inline + reified before T = correct syntax [OK]
Hint: Put 'inline' before fun and 'reified' before type param [OK]
Common Mistakes:
Placing 'reified' after the type parameter
Writing 'fun inline' instead of 'inline fun'
Omitting 'inline' keyword
3. What will be the output of the following Kotlin code?
inline fun isString(value: Any): Boolean {
return value is T
}
fun main() {
println(isString<String>("hello"))
println(isString<Int>(123))
println(isString<Double>(123.0))
}
medium
A. false\nfalse\nfalse
B. true\nfalse\ntrue
C. false\ntrue\nfalse
D. true\ntrue\ntrue
Solution
Step 1: Understand the function behavior
The function checks if the given value is of type T using the reified type parameter for runtime type check.
Step 2: Evaluate each println call
First call: isString<String>("hello") -- "hello" is String -> true. Second call: isString<Int>(123) -- 123 is Int -> true. Third call: isString<Double>(123.0) -- 123.0 is Double -> true. Output: true\ntrue\ntrue.
Final Answer:
true\ntrue\ntrue -> Option D
Quick Check:
All type checks match = true true true [OK]
Hint: Reified lets 'is' check real type at runtime [OK]
Common Mistakes:
Assuming type erasure causes false results
Mixing up types in the is check
Ignoring that reified allows runtime type check
4. Identify the error in the following Kotlin code snippet:
fun printTypeName() {
println(T::class.simpleName)
}
fun main() {
printTypeName<String>()
}
medium
A. Missing type argument when calling printTypeName.
B. T::class.simpleName is not a valid expression.
C. Cannot use reified type parameter in a non-inline function.
D. Function printTypeName must return a value.
Solution
Step 1: Check function declaration
The function uses reified type parameter but is not marked inline. This is not allowed in Kotlin.
Step 2: Understand Kotlin rules for reified
Reified type parameters require the function to be inline to preserve type info at runtime.
Final Answer:
Cannot use reified type parameter in a non-inline function. -> Option C
Quick Check:
Reified requires inline function [OK]
Hint: Reified always needs inline keyword [OK]
Common Mistakes:
Forgetting to mark function as inline
Thinking reified works in normal functions
Misunderstanding T::class usage
5. You want to create a generic inline function createInstance that returns a new instance of type T using its no-argument constructor. Which of the following implementations correctly uses reified type parameters to achieve this?
hard
A. inline fun createInstance(): T = T::class.java.newInstance()
B. fun createInstance(): T = T::class.java.getDeclaredConstructor().newInstance()
C. inline fun <T> createInstance(): T = T::class.java.newInstance()
D. inline fun createInstance(): T = T()
Solution
Step 1: Understand how to create instance with reified
Using reified lets us access T::class.java at runtime to call newInstance() for no-arg constructor.
Step 2: Check each option
inline fun createInstance(): T = T::class.java.newInstance() uses inline and reified correctly and calls newInstance(). fun createInstance(): T = T::class.java.getDeclaredConstructor().newInstance() is not inline, so reified is invalid. inline fun <T> createInstance(): T = T::class.java.newInstance() lacks reified, so T::class is invalid. inline fun createInstance(): T = T() tries to call T() which is invalid syntax in Kotlin.
Final Answer:
inline fun <reified T> createInstance(): T = T::class.java.newInstance() -> Option A