Kotlin 为什么不能将inc()定义为与接收器类型不同的类中的扩展函数?
这就是我在做Koan N28时遇到的情况:Kotlin 为什么不能将inc()定义为与接收器类型不同的类中的扩展函数?,kotlin,operator-overloading,extension-methods,extension-function,Kotlin,Operator Overloading,Extension Methods,Extension Function,这就是我在做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() } over
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()
}
}
}
}
class DateRange(覆盖值开始:MyDate,覆盖值结束包括:MyDate):ClosedRange,Iterable{
私人运营商fun MyDate.inc():MyDate{
返回这个。nextDay()
}
重写有趣的迭代器():迭代器{
返回对象:迭代器{
专用变量点=开始
override fun hasNext():Boolean{
返回点这闻起来像编译器错误。当您将扩展函数声明为成员函数时,会涉及两种接收器类型:封闭的接收器和被扩展的接收器。编译器在验证接收器时很可能看到错误的接收器。如@MarkoTopolnik所述。这似乎是编译器错误。请参阅: