Question 1 -
In the Scala documentation, I found that Traversable is a trait with an abstract method
val t = Traversable(1,2,3)
t.foreach(println _) //where is Scala picking foreach a definition from?
val v1 = Iterator(1,2,3)
v1.foreach( println _)
Question 1: When you do
Traversable(1,2,3), you are actually calling the
apply-method on the companion object of the
Traversable trait. This method actually creates a
List with the elements you provided (
foreach method you are calling is therefore actually the
foreach method of
Question 2: Yes,
Traversable is the trait at the top of the collections hierarchy in Scala.
Array are actual examples of some concrete collections, while
Traversable is a general trait that a lot of collections implement.
Iterator is not the same thing. You seem to mix them up in the question.
Iterable(1,2,3) does the exact same thing as
Traversable(1,2,3). It calls the
apply method on the companion object of
Iterable and gives you a
Iterator(1,2,3) however, calls the
apply method on
Iterator, and gives you a new
Iterator that iterates through the 3 numbers.