Scala iterator: "one should never use an iterator after calling a method on it" - why?

iterator, scala

Solution

`val remainder = it.drop(2)` may be implemented like that: it creates a new wrapper iterator that keeps a reference to the original `it` operator and advance it twice, so that the next time you call `remainder.next` you get the 3rd element. But if you then call `it.next` in between, `remainder.next` will return the 4th element...

So you have to references `remainder` and `it` that may need to call `next` and do the same side effect, which is not supported by the implementation.

Problem

Scala documentation on Iterator[T] here says the following: It is of particular importance to note that, unless stated otherwise, one should never use an iterator after calling a method on it. The two most important exceptions are also the sole abstract methods: `next` and `hasNext`. They also give a specific example of safe and unsafe use: ``` def f[A](it: Iterator[A]) = { if (it.hasNext) { // Safe to reuse "it" after "hasNext" it.next // Safe to reuse "it" after "next" val remainder = it.drop(2) // it is *not* safe to use "it" again after this line! remainder.take(2) // it is *not* safe to use "remainder" after this line! } else it } ``` Unfortunately I don't follow the idea of unsafety here. Could someone shed some light for me here?

Original source