mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
[Kotlin/en] Add more destructuring examples (#2419)
This commit is contained in:
parent
98bb8f8432
commit
7d00a22bde
@ -175,6 +175,17 @@ fun helloWorld(val name : String) {
|
||||
// Objects can be destructured into multiple variables.
|
||||
val (a, b, c) = fooCopy
|
||||
println("$a $b $c") // => 1 100 4
|
||||
|
||||
// destructuring in "for" loop
|
||||
for ((a, b, c) in listOf(fooData)) {
|
||||
println("$a $b $c") // => 1 100 4
|
||||
}
|
||||
|
||||
val mapData = mapOf("a" to 1, "b" to 2)
|
||||
// Map.Entry is destructurable as well
|
||||
for ((key, value) in mapData) {
|
||||
println("$key -> $value")
|
||||
}
|
||||
|
||||
// The "with" function is similar to the JavaScript "with" statement.
|
||||
data class MutableDataClassExample (var x: Int, var y: Int, var z: Int)
|
||||
|
Loading…
Reference in New Issue
Block a user