Note that there are some explanatory texts on larger screens.

plurals
  1. POInheritance and type parameters of Traversable
    primarykey
    data
    text
    <p>I'm studying the source code of the Scala 2.8 collection classes. I have questions about the hierarchy of <code>scala.collection.Traversable</code>. Look at the following declarations:</p> <pre><code>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] &lt;: Traversable[X]] extends HasNewBuilder[A, CC[A] @uncheckedVariance] </code></pre> <p>Question: Why does <code>Traversable</code> extend <code>GenericTraversableTemplate</code> with type parameters <code>[A, Traversable]</code> - why not <code>[A, Traversable[A]]</code>? 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 <code>Traversable[A]</code>:</p> <pre><code>error: Traversable[A] takes no type parameters, expected: one </code></pre> <p>I guess that the use of the <code>@uncheckedVariance</code> annotation in <code>GenericTraversableTemplate</code> also has to do with this? (That seems like a kind of potentially unsafe hack to force things to work...).</p> <p><em>edit</em> - found some useful answers about the annotation in <a href="https://stackoverflow.com/questions/2454281/when-is-uncheckedvariance-needed-in-scala-and-why-is-it-used-in-generictraversa">this question</a> (it is because <code>GenericTraversableTemplate</code> is used for both mutable and immutable collections which have different variance).</p> <p>Question: When you look at the hierarchy, you see that <code>Traversable</code> inherits <code>HasNewBuilder</code> twice (once via <code>TraversableLike</code> and once via <code>GenericTraversableTemplate</code>), but with slightly different type parameters. How does this work exactly? Why don't the different type parameters cause an error?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload