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
The standard library's Pair type provides component1() and component2(), so it can be destructured directly into two separate variables.
Example: 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
Because data classes automatically generate componentN() functions for their constructor properties, any data class instance can be destructured directly.
Example: 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 works naturally inside a for loop header, which is especially handy when iterating over a Map's entries as key-value pairs.
Example: 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
When only some components of a destructured value are needed, _ can be used as a placeholder for the ones you want to ignore.
Example: 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
- Trying to destructure a regular class that has not declared
componentN()functions (or is not adata class), which the compiler rejects. - Assuming destructuring order is based on property names; it is actually based on declaration order via
component1(),component2(), etc. - Using
_unnecessarily verbosely instead of skipping an unwanted component with the underscore placeholder.
val (a, b) = pairdestructures an object into separate variables using itscomponent1(),component2(), etc. functions.- Data classes automatically provide
componentN()functions for their constructor properties, making them destructurable out of the box. PairandMap.Entryare standard library types that already support destructuring.- An underscore
_can be used in place of a component you want to skip.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: