3

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.

Quinoa42
  • 41
  • 2
  • 1
    This smells like a compiler bug. When you declare an extension fun as a member function, there are two receiver types involved: the enclosing one and the one being extended. It's probable that the compiler looks at the wrong receiver when validating it. – Marko Topolnik Aug 01 '18 at 07:02
  • Like @MarkoTopolnik mentioned. This seems to be a compiler bug. See: https://youtrack.jetbrains.net/issue/KT-24800 – Alexander Egger Aug 01 '18 at 11:33

0 Answers0