Iterator [T]
here上的Scala文档说明如下:
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
andhasNext
.
他们还给出了安全和不安全使用的具体示例:
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 }
不幸的是,我不遵循这里不安全的想法.有人能在这里为我揭开光明吗?