Skip to content

Commit 7f22e3d

Browse files
cleversonzanonpivovarit
authored andcommitted
Destructuring Declarations in Kotlin - Cleverson Zanon | cleverson.ssantos1008@gmail.com (eugenp#2318)
* Different Types of Bean Injection in Spring code * Dataclasses in Kotlin * Revert "Different Types of Bean Injection in Spring code" This reverts commit 4b74772. * Destructuring Declarations in Kotlin * Corrections on Destructuring Declarations in Kotlin
1 parent 9b143b8 commit 7f22e3d

1 file changed

Lines changed: 5 additions & 16 deletions

File tree

  • kotlin/src/main/kotlin/com/baeldung/destructuringdeclarations
Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
package com.baeldung.destructuringdeclarations
22

33
import com.baeldung.destructuringdeclarations.Person
4-
import com.baeldung.destructuringdeclarations.Result
54

65
fun main(args: Array<String>) {
76

8-
//2.1. Objects
7+
//2.1. Objects
98
val person = Person(1, "Jon Snow", 20)
109
val(id, name, age) = person
1110

1211
println(id) //1
1312
println(name) //Jon Snow
1413
println(age) //20
1514

16-
//The equivalent of line 10
17-
/* val id = person.component1();
18-
val name = person.component2();
19-
val age = person.component3();*/
20-
2115
//2.2. Functions
2216
fun getPersonInfo() = Person(2, "Ned Stark", 45)
2317
val(idf, namef, agef) = getPersonInfo()
2418

25-
fun twoValuesReturn(): Result {
19+
fun twoValuesReturn(): Pair<Int, String> {
2620

2721
// needed code
2822

29-
return Result(1, "success")
23+
return Pair(1, "success")
3024
}
3125

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

4337
//2.4. Underscore and Destructuring in Lambdas
44-
val (_, status2) = twoValuesReturn()
38+
val (_, name2, age2) = person
39+
val (id3, name3) = person
4540

4641
map.mapValues { entry -> "${entry.value}!" }
4742
map.mapValues { (key, value) -> "$value!" }
4843

49-
//A pair of parameters vs. a destructuring pair
50-
/* { a -> ... } // one parameter
51-
{ a, b -> ... } // two parameters
52-
{ (a, b) -> ... } // a destructured pair
53-
{ (a, b), c -> ... } // a destructured pair and another parameter*/
54-
5544
}

0 commit comments

Comments
 (0)