0
0
Kotlinprogramming~10 mins

Reified type parameters with inline in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an inline function with a reified type parameter.

Kotlin
inline fun <[1]> printTypeName() {
    println("Type: ${T::class.simpleName}")
}
Drag options to blanks, or click blank then click option'
AT
Binline T
Creified T
Dval T
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the reified keyword causes a compilation error.
Using inline inside the angle brackets instead of before the function.
2fill in blank
medium

Complete the code to check if an object is of type T using a reified type parameter.

Kotlin
inline fun <reified T> isType(value: Any): Boolean {
    return value is [1]
}
Drag options to blanks, or click blank then click option'
AT
BAny
CUnit
DNothing
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use a non-reified type parameter in an is check causes an error.
Using a concrete type like Any instead of T.
3fill in blank
hard

Fix the error in the function to get the class name of type T using reified type parameters.

Kotlin
inline fun <reified T> getClassName(): String {
    return [1]::class.simpleName ?: "Unknown"
}
Drag options to blanks, or click blank then click option'
AT.class
BT
CT::class
DT::class.java
Attempts:
3 left
💡 Hint
Common Mistakes
Using T.class which is Java syntax and invalid in Kotlin.
Using T::class.java which returns a Java class, not Kotlin class.
4fill in blank
hard

Fill both blanks to create a function that returns a list of elements filtered by type T using reified type parameters.

Kotlin
inline fun <reified T> filterByType(list: List<Any>): List<[1]> {
    return list.filterIsInstance<[2]>()
}
Drag options to blanks, or click blank then click option'
AT
BAny
CString
DInt
Attempts:
3 left
💡 Hint
Common Mistakes
Using different types in the return type and filter function causes type mismatch.
Using concrete types like String or Int instead of the generic T.
5fill in blank
hard

Fill all three blanks to create an inline function that casts a value to type T or returns null if the cast fails.

Kotlin
inline fun <reified T> safeCast(value: Any): [1]? {
    return value as? [2] ?: [3]
}
Drag options to blanks, or click blank then click option'
AT
BAny
Cnull
DUnit
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-nullable return type causes compilation errors.
Returning Unit instead of null on cast failure.