This is what I encountered when I was doing Koan N28:
class DateRange(override val start: MyDate, override val endInclusive: MyDate) : ClosedRange<MyDate>, Iterable<MyDate> {
private operator fun MyDate.inc() : MyDate {
return this.nextDay()
}
override fun iterator(): Iterator<MyDate> {
return object : Iterator<MyDate> {
private var point = start
override fun hasNext(): Boolean {
return point <= endInclusive
}
override fun next(): MyDate {
return if (hasNext()) point++ else throw NoSuchElementException()
}
}
}
}
However, the compiler yelled at the operator
position, saying that 'operator' modifier is inapplicable on this function: receiver must be a supertype of the return type
. In order to let the code compile I have to move the extension function to the top level. But if instead I overload the MyDate.unaryPlus()
at the same place, the code does compile.