diff --git a/kotlin/src/main/kotlin/com/baeldung/destructuringdeclarations/Person.kt b/kotlin/src/main/kotlin/com/baeldung/destructuringdeclarations/Person.kt new file mode 100644 index 0000000000..d3167ce033 --- /dev/null +++ b/kotlin/src/main/kotlin/com/baeldung/destructuringdeclarations/Person.kt @@ -0,0 +1,3 @@ +package com.baeldung.destructuringdeclarations + +data class Person(var id: Int, var name: String, var age: Int) \ No newline at end of file diff --git a/kotlin/src/main/kotlin/com/baeldung/destructuringdeclarations/Result.kt b/kotlin/src/main/kotlin/com/baeldung/destructuringdeclarations/Result.kt new file mode 100644 index 0000000000..e3da9b46a4 --- /dev/null +++ b/kotlin/src/main/kotlin/com/baeldung/destructuringdeclarations/Result.kt @@ -0,0 +1,3 @@ +package com.baeldung.destructuringdeclarations + +data class Result(val result: Int, val status: String) \ No newline at end of file diff --git a/kotlin/src/main/kotlin/com/baeldung/destructuringdeclarations/Sandbox.kt b/kotlin/src/main/kotlin/com/baeldung/destructuringdeclarations/Sandbox.kt new file mode 100644 index 0000000000..87775f9378 --- /dev/null +++ b/kotlin/src/main/kotlin/com/baeldung/destructuringdeclarations/Sandbox.kt @@ -0,0 +1,55 @@ +package com.baeldung.destructuringdeclarations + +import com.baeldung.destructuringdeclarations.Person +import com.baeldung.destructuringdeclarations.Result + +fun main(args: Array) { + + //2.1. Objects + val person = Person(1, "Jon Snow", 20) + val(id, name, age) = person + + println(id) //1 + println(name) //Jon Snow + println(age) //20 + + //The equivalent of line 10 +/* val id = person.component1(); + val name = person.component2(); + val age = person.component3();*/ + + //2.2. Functions + fun getPersonInfo() = Person(2, "Ned Stark", 45) + val(idf, namef, agef) = getPersonInfo() + + fun twoValuesReturn(): Result { + + // needed code + + return Result(1, "success") + } + + // Now, to use this function: + val (result, status) = twoValuesReturn() + + //2.3. Collections and For-loops + var map: HashMap = HashMap() + map.put(1, person) + + for((key, value) in map){ + println("Key: $key, Value: $value") + } + + //2.4. Underscore and Destructuring in Lambdas + val (_, status2) = twoValuesReturn() + + map.mapValues { entry -> "${entry.value}!" } + map.mapValues { (key, value) -> "$value!" } + + //A pair of parameters vs. a destructuring pair +/* { a -> ... } // one parameter + { a, b -> ... } // two parameters + { (a, b) -> ... } // a destructured pair + { (a, b), c -> ... } // a destructured pair and another parameter*/ + +} \ No newline at end of file