Middle — Middle+
Какова роль и назначение ключевого слова reified в Kotlin?
sobes.tech AI
Answer from AI
In Kotlin, the keyword reified is used in conjunction with inline functions to preserve type information at runtime.
Typically, due to type erasure in the JVM, information about generic types is not available at runtime. But if a function is declared as inline and the type parameter is marked as reified, then inside this function, you can use the type directly, for example, for type checking or creating instances.
Example:
inline fun <reified T> isInstance(value: Any): Boolean {
return value is T
}
fun main() {
println(isInstance<String>("test")) // true
println(isInstance<Int>("test")) // false
}
Here, reified allows the use of T in the is operator.