Inheritance and type parameters of Traversable
Posted
by Jesper
on Stack Overflow
See other posts from Stack Overflow
or by Jesper
Published on 2010-04-28T23:22:46Z
Indexed on
2010/04/28
23:27 UTC
Read the original article
Hit count: 236
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...).
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?
© Stack Overflow or respective owner