我知道Traversable
,你只需要一个foreach
方法。Iterable
需要一个iterator
方法。
Scala 2.8 集合 SID 和“Fighting Bitrot with Types”论文基本上都没有提到为什么Traversable
要添加这个主题。SID 只说“David McIver...提议 Traversable 作为 Iterable 的推广”。
我从关于 IRC 的讨论中模糊地收集到它与集合遍历终止时回收资源有关吗?
以下可能与我的问题有关。中有一些看起来很奇怪的函数定义TraversableLike.scala
,例如:
def isEmpty: Boolean = {
var result = true
breakable {
for (x <- this) {
result = false
break
}
}
result
}
我认为有一个很好的理由不仅仅是写成:
def isEmpty: Boolean = {
for (x <- this)
return false
true
}