I've read
- Architecture of Scala Collections
- How are Scala collections able to return the correct collection type from a map operation?
- And of course, Is the Scala 2.8 collections library a case of "the longest suicide note in history"?
I think I have a bit of a handle on canBuildFrom
.
But then I look at the source for TraversableView
, and I see this:
object TraversableView {
class NoBuilder[A] extends Builder[A, Nothing] {
def +=(elem: A): this.type = this
def iterator: Iterator[A] = Iterator.empty
def result() = throw new UnsupportedOperationException("TraversableView.Builder.result")
def clear() {}
}
type Coll = TraversableView[_, C] forSome {type C <: Traversable[_]}
implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, TraversableView[A, Traversable[_]]] =
new CanBuildFrom[Coll, A, TraversableView[A, Traversable[_]]] {
def apply(from: Coll) = new NoBuilder
def apply() = new NoBuilder
}
}
How does TraversableView
even work like this? It seems like there is nothing happening here (NoBuilder
seems aptly named).
Could someone explain (1) the function that NoBuilder
plays here and (2) how map
, filter
, etc. can still work?