I know that to be Traversable
, you need only have a foreach
method. Iterable
requires an iterator
method.
Both the Scala 2.8 collections SID and the "Fighting Bitrot with Types" paper are basically silent on the subject of why Traversable
was added. The SID only says "David McIver... proposed Traversable as a generalization of Iterable."
I have vaguely gathered from discussions on IRC that it has to do with reclaiming resources when traversal of a collection terminates?
The following is probably related to my question. There are some odd-looking function definitions in TraversableLike.scala
, for example:
def isEmpty: Boolean = {
var result = true
breakable {
for (x <- this) {
result = false
break
}
}
result
}
I assume there's a good reason that wasn't just written as:
def isEmpty: Boolean = {
for (x <- this)
return false
true
}
Traversable
is motivated in Ch. 24 of Programming in Scala, 2nd ed.; in some cases it is easier to implement efficientlyforeach
thaniterator
. – Bosomy