Star Projection क्या है
In this page:
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
// 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"))
}
Login to try C/C++/Java/PHP code in the editor
Reading from a Star-Projected Type
एक star-projected generic type अब भी elements को सुरक्षित रूप से बाहर पढ़ने देता है, क्योंकि किसी भी element को हमेशा उसके असली type की परवाह किए बिना Any? माना जा सकता है।
उदाहरण: Reading from a Star-Projected Type
// 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))
}
Login to try C/C++/Java/PHP code in the editor
Why Writing Is Disallowed
आप किसी star-projected MutableList<*> में elements नहीं जोड़ सकते क्योंकि compiler के पास यह गारंटी देने का कोई तरीका नहीं कि आप जो भी insert करने की कोशिश करें वह list के असली, अज्ञात element type से मेल खाए।
उदाहरण: Why Writing Is Disallowed
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)
}
Login to try C/C++/Java/PHP code in the editor
Star Projection vs a Concrete Type
जब भी असली element type ज्ञात और relevant हो, List<*> के बजाय List<String> जैसे concrete generic type को प्राथमिकता दें, क्योंकि यह compiler को आपके कहीं ज़्यादा code को correctness के लिए जांचने देता है।
उदाहरण: Star Projection vs a Concrete Type
// 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)))
}
Login to try C/C++/Java/PHP code in the editor
- किसी
MutableList<*>में एक element जोड़ने की कोशिश करना, जिसे compiler reject कर देता है क्योंकि असली element type अज्ञात है, इसलिए कुछ भी सुरक्षित रूप से insert नहीं किया जा सकता। List<*>कोList<Any?>के साथ confuse करना; एक star projection ज़्यादा restrictive है क्योंकि यह असुरक्षित writes को भी रोकता है, जबकिList<Any?>किसी भी value को जोड़ने की अनुमति देता है।- 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 आमतौर पर
ischecks में उपयोग होता है, जैसे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: