I'm doing the Kotlin Koans tests in order to familiarise myself with Kotlin. In a certain test I have to override the compareTo method. In the first case everything work as intended
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) {
operator fun compareTo(other: MyDate)= when {
year != other.year -> year - other.year
month != other.month -> month - other.month
else -> dayOfMonth - other.dayOfMonth
}
}
Now in the second case where I'm writing the compareTo a little bit differently I get a ton of compilation errors.
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) {
operator fun compareTo(other: MyDate){
when {
year != other.year -> return year - other.year
month != other.month -> return month - other.month
else -> return dayOfMonth - other.dayOfMonth
}
}
}
First of all, at the operator keyword, I get an error:
'operator' modifier is inapplicable on this function: must return Int
and in the returns I get
Type mismatch: inferred type is Int but Unit was expected
I cannot understand why these errors occur since the first implementation returns the same Ints