[英]Iterator function not working on internal variable
I'm completing Kotlin Koans's For loop exercise and am wondering why: 我正在完成Kotlin Koans的For循环练习,想知道为什么:
class DateRange(val start: MyDate, val end: MyDate) : Iterable<MyDate> {
override fun iterator(): Iterator<MyDate> = DateIterator(this)
}
class DateIterator(val daterange: DateRange) : Iterator<MyDate> {
var currentDay: MyDate = daterange.start
override fun hasNext(): Boolean = currentDay.next() <= daterange.end
override fun next(): MyDate {
val result = currentDay
currentDay = currentDay.nextDay()
return result
}
}
I'm receiving an Unresolved reference: next
on currentDay()
. 我收到一个Unresolved reference: next
在currentDay()
。
I'm aware the answer to this question is the exact same code without next()
, but am wondering why it doesn't work - considering currentDay
is a MyDate
object and next()
returns a MyDate
. 我知道这个问题的答案是没有next()
代码完全相同,但是我想知道为什么它不起作用-考虑到currentDay
是MyDate
对象,而next()
返回MyDate
。
Any idea why? 知道为什么吗?
considering
currentDay
is aMyDate
object andnext()
returns aMyDate
考虑currentDay
是MyDate
对象,next()
返回MyDate
But there is no method next()
on the MyDate
class (which is what the error message tries to say). 但是MyDate
类上没有方法next()
(错误消息试图说的是)。 So currentDay.next()
will not compile. 因此currentDay.next()
将不会编译。
There is only next()
on the DateIterator
class (but of course you don't want to call that as it would have the side-effect of advancing the iterator). DateIterator
类上仅存在next()
(但是,您当然不希望调用它,因为它具有推进迭代器的副作用)。
You must have meant to call currentDay.nextDay()
instead. 您必须打算改为调用currentDay.nextDay()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.