hasNext()
on an Iterator
should not have any side-effects KT-W1032The hasNext()
method in Kotlin's Iterator
interface is used to check if there are any more elements in the collection that can be iterated over.
This method should have no side-effects and should not modify the underlying collection or iterator state.
If hasNext()
has side-effects or modifies the iterator state, it can lead to unexpected behavior and incorrect results when iterating over the collection.
This can introduce bugs and make the code harder to understand and maintain.
class MyIterator : Iterator<String> {
override fun hasNext(): Boolean {
return next() != null // Here, the `next()` method is being called inside `hasNext()` which changes the state of the iterator
}
}
Do not have any logic inside the hasNext()
method that would alter the collection or the iterator state.