この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
はじめに
毎日Kotlinシリーズです。
このシリーズを初めての方はこちらです。「毎日Kotlin」はじめました | Developers.IO
問題
Destructuring declarations | Try Kotlin
Read about destructuring declarations and make the following code compile by adding one word.
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
}
val (year, month, dayOfMonth) = dateこんな感じの記述で要素ごとに取得できたらいいなぁっという問題です。
狙い
ここで考えて欲しい問題の意図はなんだろうか?
以前やりましたね?覚えてるかな?
解答例
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int)
dataをつけるだけ。簡単すぎる。あれなんでこれができるんだっけ?っと忘れてしまった方はこちらをみてみよう!
[毎日Kotlin] Day7. Data classes(データクラス)
あとがき
Day21.でまたお会いしましょう。