Challenge - 5 Problems
Reified Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of inline function with reified type
What is the output of this Kotlin code snippet?
Kotlin
inline fun <reified T> printTypeName() { println(T::class.simpleName) } fun main() { printTypeName<String>() printTypeName<Int>() }
Attempts:
2 left
💡 Hint
Reified type parameters allow access to the actual type at runtime inside inline functions.
✗ Incorrect
The inline function with reified type parameter T can access T::class.simpleName, which returns the simple name of the type. So calling printTypeName() prints "String" and printTypeName() prints "Int".
🧠 Conceptual
intermediate2:00remaining
Why use reified type parameters with inline functions?
Which of the following best explains why Kotlin requires the
inline modifier when using reified type parameters?Attempts:
2 left
💡 Hint
Think about how Kotlin handles generics and type erasure.
✗ Incorrect
Kotlin erases generic types at runtime. Using
inline functions with reified type parameters allows the compiler to replace the generic type with the actual type during compilation, enabling type checks and casts inside the function.🔧 Debug
advanced2:00remaining
Identify the error in this code using reified type parameter
What error does this Kotlin code produce?
Kotlin
fun <reified T> checkType(value: Any) { if (value is T) { println("Value is of type T") } else { println("Value is NOT of type T") } } fun main() { checkType<String>(123) }
Attempts:
2 left
💡 Hint
Check the function declaration for inline keyword.
✗ Incorrect
Reified type parameters require the function to be marked as inline. Without 'inline', the compiler cannot access the actual type at runtime and will produce a compilation error.
📝 Syntax
advanced2:00remaining
Correct syntax to use reified type parameter in inline function
Which option shows the correct syntax to declare an inline function with a reified type parameter and check if an object is of that type?
Attempts:
2 left
💡 Hint
The order of keywords and placement of 'reified' matters.
✗ Incorrect
The correct syntax places 'inline' before 'fun' and 'reified' before the type parameter T inside angle brackets. Option B follows this syntax correctly.
🚀 Application
expert2:00remaining
Using reified type parameters to filter a list
Given this Kotlin function, what is the output when filtering a mixed list for elements of type String?
Kotlin
inline fun <reified T> List<Any>.filterType(): List<T> { return this.filterIsInstance<T>() } fun main() { val mixedList = listOf(1, "apple", 2.5, "banana", 3) val strings = mixedList.filterType<String>() println(strings) }
Attempts:
2 left
💡 Hint
filterIsInstance() returns elements of type T from the list.
✗ Incorrect
The function filterType uses filterIsInstance() which returns a list of elements that are instances of T. Here, filtering for String returns only the string elements "apple" and "banana".