Destructuring Declarations क्या हैं
In this page:
val (a, b) = pair
val (prop1, prop2) = dataClassInstance
for ((key, value) in map) {
// use key and value
}
Destructuring a Pair
standard library का Pair type component1() और component2() प्रदान करता है, इसलिए इसे सीधे दो अलग variables में destructure किया जा सकता है।
उदाहरण: Destructuring a Pair
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `pair`, set to `"Kotlin" to 2011`
val pair = "Kotlin" to 2011
val (language, year) = pair
// Print "$language was released in $year" to the console, with a trailing newline
println("$language was released in $year")
}
Login to try C/C++/Java/PHP code in the editor
Destructuring a Data Class
चूंकि data classes अपनी constructor properties के लिए अपने आप componentN() functions generate करती हैं, किसी भी data class instance को सीधे destructure किया जा सकता है।
उदाहरण: Destructuring a Data Class
// Define a data class `Point`
data class Point(val x: Int, val y: Int)
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `point`, set to `Point(3, 4)`
val point = Point(3, 4)
val (x, y) = point
// Print "x=$x, y=$y" to the console, with a trailing newline
println("x=$x, y=$y")
}
Login to try C/C++/Java/PHP code in the editor
Destructuring in for Loops
Destructuring किसी for loop header के अंदर स्वाभाविक रूप से काम करता है, जो किसी Map की entries को key-value pairs के रूप में iterate करते समय खासतौर पर सुविधाजनक है।
उदाहरण: Destructuring in for Loops
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `scores`, set to `mapOf("Ana" to 90, "Bo" to 85)`
val scores = mapOf("Ana" to 90, "Bo" to 85)
for ((name, score) in scores) {
// Print "$name scored $score" to the console, with a trailing newline
println("$name scored $score")
}
}
Login to try C/C++/Java/PHP code in the editor
Skipping Components with Underscore
जब किसी destructured value के सिर्फ कुछ components चाहिए हों, तो _ उन components के लिए एक placeholder के रूप में उपयोग किया जा सकता है जिन्हें आप नज़रअंदाज़ करना चाहते हैं।
उदाहरण: Skipping Components with Underscore
// Define a data class `Point3D`
data class Point3D(val x: Int, val y: Int, val z: Int)
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `point`, set to `Point3D(1, 2, 3)`
val point = Point3D(1, 2, 3)
val (x, _, z) = point
// Print "x=$x, z=$z" to the console, with a trailing newline
println("x=$x, z=$z")
}
Login to try C/C++/Java/PHP code in the editor
- किसी regular class को destructure करने की कोशिश करना जिसने
componentN()functions declare नहीं किए हैं (या जोdata classनहीं है), जिसे compiler reject कर देता है। - यह मान लेना कि destructuring order property names पर आधारित है; यह असल में
component1(),component2(), आदि के ज़रिए declaration order पर आधारित है। - अनावश्यक रूप से verbosely
_उपयोग करना बजाय underscore placeholder से किसी अवांछित component को skip करने के।
val (a, b) = pairकिसी object को उसकेcomponent1(),component2(), आदि functions का उपयोग करके अलग variables में destructure करता है।- Data classes अपनी constructor properties के लिए अपने आप
componentN()functions प्रदान करती हैं, उन्हें out of the box destructurable बनाते हुए। PairऔरMap.Entrystandard library types हैं जो पहले से destructuring support करते हैं।- एक underscore
_किसी ऐसे component की जगह उपयोग किया जा सकता है जिसे आप skip करना चाहते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: