Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 762403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:57:15+00:00 2026-05-14T15:57:15+00:00

I’m studying the source code of the Scala 2.8 collection classes. I have questions

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:57:16+00:00Added an answer on May 14, 2026 at 3:57 pm

    The reason is the CC parameter in the GenericTraversableTemplate trait. Unlike a normal type parameter which has kind * (pronounced “type”), this parameter has type * => * (pronounced “type to type”). In order to understand what this means, you first need to have a little background about kinds.

    Consider the following snippet:

    val a: Int = 42
    

    Here we see 42, which is a value. Values have intrinsic types. In this case, our value is 42 and the type is Int. A type is something like a category which encompasses many values. It says something about the values which are possible for variable a. For example, we know that a cannot contain the value "foobar", because that value has type String. Thus, values are sort of like the first level of abstraction, while types are one level above values.

    So here’s the question: what’s stopping us from taking this one step further? If values can have types, why can’t types have “something” above them? That “something” is called a kind. Kinds are to types what types are to values, generic categories which restrict what sort of types can be described.

    Let’s look at some concrete examples:

    type String
    type Int
    type List[Int]
    

    These are types, and they all have kind *. This is the most common kind (which is why we call it “type”). In practice, most types have this kind. However, some do not:

    type List     // note: compile error
    

    Here we have the type constructor List, but this time we “forgot” to specify its type parameter. As it turns out, this is actually a type, but one of a different kind. Specifically, * => *. As the notation is meant to imply, this kind describes a type which takes another type of kind * as a parameter, producing a new type of kind * as a result. We can see this in the first example, where we passed the Int type (which has kind *) to the List type constructor (which has kind * => *), producing the type List[Int] (which has kind *).

    Returning to GenericTraversableTemplate, let’s look again at the declaration:

    trait GenericTraversableTemplate[+A, +CC[X] <: Traversable[X]]
    

    Notice how the CC type parameter takes a parameter of its own, but that parameter is not defined by any other type parameter in the declaration? This is Scala’s rather clumsy way of saying that CC must be of kind * => * (just like a must be of type Int in our earlier example). “Normal” type parameters (such as A) are always of kind *. By forcing CC to be of kind * => *, we’re effectively telling the compiler that the only valid types which can be substituted for this parameter must be themselves of kind * => *. Thus:

    type GenericTraversableTemplate[String, List]        // valid!
    type GenericTraversableTemplate[String, List[Int]]   // invalid!
    

    Remember, List is of kind * => * (exactly what we need for CC), but List[Int] has kind *, so the compiler rejects it.

    For the record, GenericTraversableTemplate itself has a kind, specifically: (* x (* => *)) => *. This means that GenericTraversableTemplate is a type which takes two types as parameters — one of kind *, the other of kind * => * — and produces a type of kind * as a result. In our example above, GenericTraversableTemplate[String, List] is one such result type, and as we computed, it is of kind * (it takes no parameters).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.