Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
package com.baeldung.destructuringdeclarations

import com.baeldung.destructuringdeclarations.Person
import com.baeldung.destructuringdeclarations.Result

fun main(args: Array<String>) {

//2.1. Objects
//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 {
fun twoValuesReturn(): Pair<Int, String> {

// needed code

return Result(1, "success")
return Pair(1, "success")
}

// Now, to use this function:
Expand All @@ -41,15 +35,10 @@ fun main(args: Array<String>) {
}

//2.4. Underscore and Destructuring in Lambdas
val (_, status2) = twoValuesReturn()
val (_, name2, age2) = person
val (id3, name3) = person

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*/

}