january
january copied to clipboard
Gotcha: hasNext() modifies state
Ooh, this just caught me out.
In Java the iterator.hasNext()
method is always a pure function: it doesn't modify state. So, until iterator.next()
is called, it will always return the same value.
However, in January iterator.hasNext()
is used to advance the iterator, since the actual value is returned from a public int value.
Maybe the above strategy has been introduced as a performance measure. Does this outweigh the counter-intuitive implementation of iterator.hasNext()
?
I propose that January iterators follow the Java standard of hasNext()
for loop control and next()
to both retrieve the current value and progress the iterator.