data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {
    override fun compareTo(other: MyDate) = when {
        year != other.year -> year - other.year
        month != other.month -> month - other.month
        else -> dayOfMonth - other.dayOfMonth
    }
}

fun test(date1: MyDate, date2: MyDate) {
    // this code should compile:
    println(date1 < date2)
}

https://play.kotlinlang.org/koans/Conventions/Comparison/Task.kt

코틀린의 when expression은 정말 강력합니다

파이썬은 3.10에 와서야 패턴매칭이 지원됐는데.. 코틀린은 ㅎㅎ 바로 되네요

반응형