Scala: what is trait TraversableOnce? What's the different between TraversableOnce and Traversable?

The Scaladoc also says

This trait exists primarily to eliminate code duplication between Iterator and Traversable, and thus implements some of the common methods that can be implemented solely in terms of foreach without access to a Builder.

Iterators can only be 'traversed' once. A Traversable can be traversed many times.

Essentially, TraversableOnce is an interface that abstracts away how you handle Iterators and Traversables. Your code could receive either an Iterator or a Traversable and handle them in exactly the same way!

For a good explanation of many of the traits used in the Collections library, I believe the majority (if not all) of the Scala 2.8 Collections Design Tutorial is still correct.

Tags:

Scala