Skip to content

Commit 7ffd8ec

Browse files
cleversonzanonpivovarit
authored andcommitted
Cleverson Zanon | cleverson.ssantos1008@gmail.com (eugenp#2315)
* 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
1 parent d4f245a commit 7ffd8ec

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.destructuringdeclarations
2+
3+
data class Person(var id: Int, var name: String, var age: Int)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.destructuringdeclarations
2+
3+
data class Result(val result: Int, val status: String)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.destructuringdeclarations
2+
3+
import com.baeldung.destructuringdeclarations.Person
4+
import com.baeldung.destructuringdeclarations.Result
5+
6+
fun main(args: Array<String>) {
7+
8+
//2.1. Objects
9+
val person = Person(1, "Jon Snow", 20)
10+
val(id, name, age) = person
11+
12+
println(id) //1
13+
println(name) //Jon Snow
14+
println(age) //20
15+
16+
//The equivalent of line 10
17+
/* val id = person.component1();
18+
val name = person.component2();
19+
val age = person.component3();*/
20+
21+
//2.2. Functions
22+
fun getPersonInfo() = Person(2, "Ned Stark", 45)
23+
val(idf, namef, agef) = getPersonInfo()
24+
25+
fun twoValuesReturn(): Result {
26+
27+
// needed code
28+
29+
return Result(1, "success")
30+
}
31+
32+
// Now, to use this function:
33+
val (result, status) = twoValuesReturn()
34+
35+
//2.3. Collections and For-loops
36+
var map: HashMap<Int, Person> = HashMap()
37+
map.put(1, person)
38+
39+
for((key, value) in map){
40+
println("Key: $key, Value: $value")
41+
}
42+
43+
//2.4. Underscore and Destructuring in Lambdas
44+
val (_, status2) = twoValuesReturn()
45+
46+
map.mapValues { entry -> "${entry.value}!" }
47+
map.mapValues { (key, value) -> "$value!" }
48+
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+
55+
}

0 commit comments

Comments
 (0)