Hanbit the Developer
Kotlin Documentation | Destructuring declarations 본문
Category: Concepts
문서 링크: https://kotlinlang.org/docs/destructuring-declarations.html
val (name, age) = person
위 코드는 아래 코드로 컴파일된다.
val name = person.component1()
val age = person.component2()
for문에서도 적용된다.
for ((key, value) in map) {
// do something with the key and the value
}
Underscore for unused variables
val (_, status) = getResult()
Destructuring in lambdas
{ a -> ... } // one parameter
{ a, b -> ... } // two parameters
{ (a, b) -> ... } // a destructured pair
{ (a, b), c -> ... } // a destructured pair and another parameter
타입 명시:
map.mapValues { (_, value): Map.Entry<Int, String> -> "$value!" }
map.mapValues { (_, value: String) -> "$value!" }
'Kotlin' 카테고리의 다른 글
Kotlin Documentation | Classes (0) | 2023.05.22 |
---|---|
Kotlin Documentation | Reflection (0) | 2023.05.22 |
Kotlin Documentation | Annotations (0) | 2023.05.22 |
Kotlin Documentation | Asynchronous programming techniques (0) | 2023.05.22 |
Kotlin Documentation | Equality (0) | 2023.05.22 |