← Back to Kotlin Course | Chapter 11: Generics | Lesson 5 of 6

Type Erasure क्या है

Type erasure का मतलब है कि एक बार आपका code compile हो जाए, JVM बिल्कुल भूल जाता है कि कोई container कौन सा generic type रखता था, सिर्फ container को खुद याद रखते हुए।
Syntax
kotlin
// 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

markup
// 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}")
}

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>

markup
// 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"))
}

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

markup
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)}")
}

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

markup
// 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}")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. if (list is List<String>) जांचने की कोशिश करना, जिसे compiler reject कर देता है क्योंकि element type erase हो जाता है और runtime पर जांचा नहीं जा सकता।
  2. यह मान लेना कि दो List<Int> और List<String> अलग runtime classes हैं; runtime पर वे दोनों बस एक ही List class हैं।
  3. यह भूल जाना कि 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 नहीं, और यही वजह है कि reified type 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.