In Scala 2.8 collections, why was the Traversable type added above Iterable?
Posted
by Seth Tisue
on Stack Overflow
See other posts from Stack Overflow
or by Seth Tisue
Published on 2010-04-08T18:16:35Z
Indexed on
2010/04/08
18:23 UTC
Read the original article
Hit count: 212
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
}
© Stack Overflow or respective owner