Kotlin Koans 18 Destructuring declarations

Contents

Destructuring declarations (Playground)

Description

Read about destructuring declarations and make the following code compile by adding one word.

破壊宣言を読み、1ワード追加して次のコードがコンパイルできるようにしてください。

Code

data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int)

fun isLeapDay(date: MyDate): Boolean {

    val (year, month, dayOfMonth) = date

    // 29 February of a leap year
    return year % 4 == 0 && month == 2 && dayOfMonth == 29
}

Memo

val (name, age) = person

// 以下にコンパイルされる
val name = person.component1()
val age = person.component2()

← Posts