I'm studying the source code of the Scala 2.8 collection classes. I have questions about the hierarchy of scala.collection.Traversable
. Look at the following declarations:
package scala.collection
trait Traversable[+A]
extends TraversableLike[A, Traversable[A]]
with GenericTraversableTemplate[A, Traversable]
trait TraversableLike[+A, +Repr]
extends HasNewBuilder[A, Repr]
with TraversableOnce[A]
package scala.collection.generic
trait HasNewBuilder[+A, +Repr]
trait GenericTraversableTemplate[+A, +CC[X] <: Traversable[X]]
extends HasNewBuilder[A, CC[A] @uncheckedVariance]
Question: Why does Traversable
extend GenericTraversableTemplate
with type parameters [A, Traversable]
- why not [A, Traversable[A]]
? I tried some experimenting with a small program with the same structure and got a strange error message when I tried to change it to Traversable[A]
:
error: Traversable[A] takes no type parameters, expected: one
I guess that the use of the @uncheckedVariance
annotation in GenericTraversableTemplate
also has to do with this? (That seems like a kind of potentially unsafe hack to force things to work...).
edit - found some useful answers about the annotation in this question (it is because GenericTraversableTemplate
is used for both mutable and immutable collections which have different variance).
Question: When you look at the hierarchy, you see that Traversable
inherits HasNewBuilder
twice (once via TraversableLike
and once via GenericTraversableTemplate
), but with slightly different type parameters. How does this work exactly? Why don't the different type parameters cause an error?