Reified Type Parameters क्या हैं
In this page:
inline fun <reified T> functionName(value: Any): Boolean {
return value is T // T is available at runtime
}
inline fun <reified T> typeName(): String? = T::class.simpleName
The Type Erasure Problem
JVM पर, generic type information आमतौर पर runtime पर erase हो जाती है, इसलिए एक regular generic function if (value is T) नहीं जांच सकता -- compiler को compile होने के बाद यह पता नहीं होता कि T असल में क्या है।
उदाहरण: The Type Erasure Problem
fun <T> describe(value: Any): String {
// Return `"Checking value: $value"` from this function
return "Checking value: $value"
}
// Entry point: execution of the program starts here
fun main() {
// Print `describe<String>("Kotlin")` to the console, with a trailing newline
println(describe<String>("Kotlin"))
}
Login to try C/C++/Java/PHP code in the editor
Reified Solves It with inline
किसी inline function पर type parameter को reified चिह्नित करना हर call site पर असली type को संरक्षित करता है, क्योंकि function का code (और असली type) सीधे caller में copy हो जाते हैं।
उदाहरण: Reified Solves It with inline
inline fun <reified T> isInstanceOf(value: Any): Boolean = value is T
// Entry point: execution of the program starts here
fun main() {
// Print `isInstanceOf<String>("Kotlin")` to the console, with a trailing newline
println(isInstanceOf<String>("Kotlin"))
// Print `isInstanceOf<Int>("Kotlin")` to the console, with a trailing newline
println(isInstanceOf<Int>("Kotlin"))
}
Login to try C/C++/Java/PHP code in the editor
Using T::class with Reified
एक reified type parameter सीधे T::class access करने की भी अनुमति देता है, जो logging, reflection-lite checks, या generic factories बनाने के लिए उपयोगी है।
उदाहरण: Using T::class with Reified
inline fun <reified T> typeName(): String = T::class.simpleName ?: "Unknown"
// Entry point: execution of the program starts here
fun main() {
// Print `typeName<String>()` to the console, with a trailing newline
println(typeName<String>())
// Print `typeName<Int>()` to the console, with a trailing newline
println(typeName<Int>())
}
Login to try C/C++/Java/PHP code in the editor
Filtering a List by Reified Type
Reified generics का एक आम practical उपयोग किसी मिश्रित collection को सिर्फ एक specific runtime-checked type के elements तक filter करना है।
उदाहरण: Filtering a List by Reified Type
inline fun <reified T> filterByType(items: List<Any>): List<T> = items.filterIsInstance<T>()
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `mixed`, set to `listOf(1, "two", 3, "four", 5)`
val mixed = listOf(1, "two", 3, "four", 5)
// Declare a read-only variable `strings`, set to `filterByType<String>(mixed)`
val strings = filterByType<String>(mixed)
// Print `strings` to the console, with a trailing newline
println(strings)
}
Login to try C/C++/Java/PHP code in the editor
- किसी regular (non-inline) generic function में
T::classयाis Tउपयोग करने की कोशिश करना, जो compile होने में fail होता है क्योंकि type runtime पर erase हो जाता है। - यह भूल जाना कि
reifiedसिर्फ किसीinlinefunction के type parameter पर उपयोग किया जा सकता है, क्योंकि type information inlining से संरक्षित होती है, खुद JVM से नहीं। - reified generics का ज़्यादा उपयोग करना जहां explicitly पास किया एक साधारण
Class<T>parameter भी उतना ही अच्छा काम करता और function को non-inline रखता।
- किसी
inlinefunction पर type parameter कोreifiedचिह्नित करना असली type को उस function के अंदर runtime पर जांचने और उपयोग करने देता है। reifiedसिर्फ inline functions पर अनुमति है, क्योंकि inlining ही हर call site पर असली type information को संरक्षित करता है।- Reified type parameters
is TऔरT::classजैसे operations को संभव बनाते हैं जो आमतौर पर JVM type erasure की वजह से असंभव हैं। - एक आम उपयोग एक generic helper function है जो किसी collection को runtime-provided type से filter या जांचता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: