[Kotlin/en] Add more destructuring examples (#2419)

This commit is contained in:
Sergey Mashkov 2016-10-05 17:59:16 +03:00 committed by ven
parent 98bb8f8432
commit 7d00a22bde

View File

@ -176,6 +176,17 @@ fun helloWorld(val name : String) {
val (a, b, c) = fooCopy val (a, b, c) = fooCopy
println("$a $b $c") // => 1 100 4 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. // The "with" function is similar to the JavaScript "with" statement.
data class MutableDataClassExample (var x: Int, var y: Int, var z: Int) data class MutableDataClassExample (var x: Int, var y: Int, var z: Int)
val fooMutableDate = MutableDataClassExample(7, 4, 9) val fooMutableDate = MutableDataClassExample(7, 4, 9)