Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lost in the inheritance graph of Scala's collections

Today I wanted to learn about the supertypes of List:

sealed abstract class List[+A] extends AbstractSeq[A]
                                  with LinearSeq[A]
                                  with Product
                                  with GenericTraversableTemplate[A, List]
                                  with LinearSeqOptimized[A, List[A]]

Wow, so List has already five immediate supertypes. Let's pick one at random:

trait LinearSeq[+A] extends Seq[A]
                       with scala.collection.LinearSeq[A]
                       with GenericTraversableTemplate[A, LinearSeq]
                       with LinearSeqLike[A, LinearSeq[A]]

Okay, let's pick the one with the most similar name:

trait LinearSeqLike[+A, +Repr <: LinearSeqLike[A, Repr]] extends SeqLike[A, Repr]

Ah, it seems we're getting somewhere, only one supertype left:

trait SeqLike[+A, +Repr] extends Any
                            with IterableLike[A, Repr]
                            with GenSeqLike[A, Repr]
                            with Parallelizable[A, ParSeq[A]]

At this point I gave up. How deep does this graph go? Which of all these supertypes are conceptually relevant, and which of them are just implementation details or optimization tricks?

How does one make sense of such an enormous inheritance graph?

like image 630
fredoverflow Avatar asked Apr 28 '13 21:04

fredoverflow


2 Answers

Most parents are indeed implementation details and optimization tricks. If you don't care about it, you can ignore anything with Like or Template at the end. Applying that to lists, we have: List <: LinearSeq <: Seq <: Iterable <: Traversable. You should use these traits as arguments types in your code (rather than the implementation traits). They are decribed in: Scala Collections API

If you want to understand how the implementations traits are used, or design your own collections, you should read this tutorial: The Architecture of Scala Collections.

Moreover, if you want/need to know where a given method is actually implemented, click on the method signature in the scaladoc to expand de description. The Definition Classes field shows links to the implementation location.

like image 164
paradigmatic Avatar answered Oct 24 '22 06:10

paradigmatic


Once you reached the SeqLike, you're almost there -- IterableLike has just GenIterableLike and TraversableLike above it, TraversableLike only has TraversableOnce and GenTraversableLike above.

And TraversableOnce has GenTraversableOnce above it, and that's it :)

The link that the other user provided (also http://docs.scala-lang.org/overviews/collections/overview.html) is a good reference -- you just need to know that each of those types have a corresponding *Like type that holds the representation type parameter Repr.

But from the point of view of extending collections, you just need to find the most specific type you want to extend in most cases, for example SeqLike and extend Seq[T] and Seq[T, YourCollectionType[T]].

In the future versions of Scala the Gen* traits might be removed, though, making the hierarchy simpler.

like image 1
axel22 Avatar answered Oct 24 '22 06:10

axel22