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

Star Projection क्या है

एक star projection Kotlin का यह कहने का तरीका है 'मुझे परवाह नहीं कि यह generic specifically कौन सा type रख रहा है, मुझे बस इसके साथ generically काम करना है'।
Syntax
kotlin
fun functionName(value: Any) {
    if (value is List<*>) {     // any element type
        // can read elements as Any?
    }
}

fun read(items: List<*>) { }

Using Star Projection in Type Checks

चूंकि generic type arguments runtime पर erase हो जाते हैं, यह जांचना कि कोई चीज़ अज्ञात element type की List है या नहीं star projection syntax List<*> उपयोग करता है।

उदाहरण: Using Star Projection in Type Checks

markup
// Define the function `printSize` taking `value`
fun printSize(value: Any) {
    // Check whether `value is List<*>`
    if (value is List<*>) {
        // Print "List size: ${value.size}" to the console, with a trailing newline
        println("List size: ${value.size}")
    }
}

// Entry point: execution of the program starts here
fun main() {
    printSize(listOf(1, 2, 3))
    printSize(listOf("a", "b"))
}

Reading from a Star-Projected Type

एक star-projected generic type अब भी elements को सुरक्षित रूप से बाहर पढ़ने देता है, क्योंकि किसी भी element को हमेशा उसके असली type की परवाह किए बिना Any? माना जा सकता है।

उदाहरण: Reading from a Star-Projected Type

markup
// Define the function `printAll` taking `items`
fun printAll(items: List<*>) {
    // Loop over `items`, binding each item to `item`
    for (item in items) {
        // Print `item` to the console, with a trailing newline
        println(item)
    }
}

// Entry point: execution of the program starts here
fun main() {
    printAll(listOf(1, "two", 3.0))
}

Why Writing Is Disallowed

आप किसी star-projected MutableList<*> में elements नहीं जोड़ सकते क्योंकि compiler के पास यह गारंटी देने का कोई तरीका नहीं कि आप जो भी insert करने की कोशिश करें वह list के असली, अज्ञात element type से मेल खाए।

उदाहरण: Why Writing Is Disallowed

markup
fun describeMutable(list: MutableList<*>) {
    // list.add("something") would not compile here
    println("A mutable list with ${list.size} elements of an unknown type")
}

fun main() {
    val numbers = mutableListOf(1, 2, 3)
    describeMutable(numbers)
}

Star Projection vs a Concrete Type

जब भी असली element type ज्ञात और relevant हो, List<*> के बजाय List<String> जैसे concrete generic type को प्राथमिकता दें, क्योंकि यह compiler को आपके कहीं ज़्यादा code को correctness के लिए जांचने देता है।

उदाहरण: Star Projection vs a Concrete Type

markup
// Define the function `sumConcrete` taking `numbers` and returning an Int
fun sumConcrete(numbers: List<Int>): Int = numbers.sum()

// Entry point: execution of the program starts here
fun main() {
    // Print `sumConcrete(listOf(1, 2, 3, 4))` to the console, with a trailing newline
    println(sumConcrete(listOf(1, 2, 3, 4)))
}
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. किसी MutableList<*> में एक element जोड़ने की कोशिश करना, जिसे compiler reject कर देता है क्योंकि असली element type अज्ञात है, इसलिए कुछ भी सुरक्षित रूप से insert नहीं किया जा सकता।
  2. List<*> को List<Any?> के साथ confuse करना; एक star projection ज़्यादा restrictive है क्योंकि यह असुरक्षित writes को भी रोकता है, जबकि List<Any?> किसी भी value को जोड़ने की अनुमति देता है।
  3. star projection का ज़्यादा उपयोग करना जब असली type parameter पहले से ज्ञात हो और एक concrete generic type ज़्यादा साफ़ और सुरक्षित होता।
चैप्टर सारांश
  • Type<*> (एक star projection) किसी generic type को represent करता है जब exact type argument अज्ञात या अनावश्यक हो।
  • एक star-projected generic type सुरक्षित reading की अनुमति देता है (किसी supertype जैसे Any? की) लेकिन writing की नहीं, क्योंकि compiler insertion के लिए type safety verify नहीं कर सकता।
  • Star projection आमतौर पर is checks में उपयोग होता है, जैसे is List<*>, क्योंकि erasure असली element type जांचने से रोकता है।
  • List<*> अज्ञात element type की एक read-only list जैसा व्यवहार करता है, List<Any?> से अलग जो explicitly किसी भी type की अनुमति देता है।
🔒

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.