Type Erasure क्या है
In this page:
// Erased at runtime: cannot check the type argument
// if (list is List<String>) // error
if (list is List<*>) { /* only the raw type is checked */ }
// reified keeps the type argument inside an inline function
inline fun <reified T> functionName(value: Any) = value is T
Generic Type Information Is Erased
एक बार compile होने के बाद, JVM List<String> और List<Int> में अंतर नहीं करता -- दोनों runtime पर बस एक साधारण List के रूप में represent होते हैं, जो 'type erasure' का मतलब है।
उदाहरण: Generic Type Information Is Erased
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `strings` (a `List<Any>`), set to `listOf("a", "b")`
val strings: List<Any> = listOf("a", "b")
// Declare a read-only variable `numbers` (a `List<Any>`), set to `listOf(1, 2)`
val numbers: List<Any> = listOf(1, 2)
// Print "Same runtime class: ${strings::class == numbers::class}" to the console, with a trailing newline
println("Same runtime class: ${strings::class == numbers::class}")
}
Login to try C/C++/Java/PHP code in the editor
Why You Cannot Check is List<String>
चूंकि element type erase हो जाता है, Kotlin is List<String> को सीधे जांचने की अनुमति नहीं देता -- सिर्फ यह कि कोई चीज़ बिल्कुल एक List है या नहीं, is List<*> जैसे star projection का उपयोग करके।
उदाहरण: Why You Cannot Check is List<String>
// Define the function `describe` taking `value`
fun describe(value: Any) {
// Check whether `value is List<*>`
if (value is List<*>) {
// Print "It is some kind of List with ${value.size} elements" to the console, with a trailing newline
println("It is some kind of List with ${value.size} elements")
}
}
// Entry point: execution of the program starts here
fun main() {
describe(listOf(1, 2, 3))
describe(listOf("a", "b"))
}
Login to try C/C++/Java/PHP code in the editor
Working Around Erasure with reified
चूंकि साधारण generic functions runtime पर असली type नहीं देख सकते, किसी inline function के type parameter पर reified keyword standard workaround है जब genuinely एक runtime type check चाहिए।
उदाहरण: Working Around Erasure with reified
inline fun <reified T> countOfType(items: List<Any>): Int = items.count { it is T }
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `mixed`, set to `listOf(1, "a", 2, "b", 3)`
val mixed = listOf(1, "a", 2, "b", 3)
// Print "Number of Strings: ${countOfType<String>(mixed)}" to the console, with a trailing newline
println("Number of Strings: ${countOfType<String>(mixed)}")
}
Login to try C/C++/Java/PHP code in the editor
Arrays Are Not Erased
Generic collections के विपरीत, Kotlin में arrays (JVM arrays से backed) runtime पर अपनी component type information बनाए रखते हैं, इसलिए is Array<String>-style checks generic collections से अलग व्यवहार करते हैं।
उदाहरण: Arrays Are Not Erased
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `stringArray` (a `Array<String>`), set to `arrayOf("a", "b")`
val stringArray: Array<String> = arrayOf("a", "b")
// Print "Array component type: ${stringArray.javaClass.componentType}" to the console, with a trailing newline
println("Array component type: ${stringArray.javaClass.componentType}")
}
Login to try C/C++/Java/PHP code in the editor
if (list is List<String>)जांचने की कोशिश करना, जिसे compiler reject कर देता है क्योंकि element type erase हो जाता है और runtime पर जांचा नहीं जा सकता।- यह मान लेना कि दो
List<Int>औरList<String>अलग runtime classes हैं; runtime पर वे दोनों बस एक हीListclass हैं। - यह भूल जाना कि arrays एक exception हैं -- generic collections के विपरीत, arrays runtime पर अपनी element type information बनाए रखते हैं (natively JVM द्वारा reified)।
- Runtime पर, generic type parameters erase हो जाते हैं -- एक
List<String>और एकList<Int>compile होने के बाद दोनों बसListहैं। - आप
is List<String>को सीधे नहीं जांच सकते; सिर्फ एक unchecked cast याis List<*>जैसी star-projected check की अनुमति है। - Type erasure एक JVM-wide limitation है, Kotlin के लिए specific नहीं, और यही वजह है कि
reifiedtype parameters (inlineके साथ) एक workaround के रूप में मौजूद हैं। - Arrays erasure का एक exception हैं -- उनका component type JVM द्वारा runtime पर संरक्षित रखा जाता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: