Mixing multiple traits in Scala

ScalaMixinsTraits

Scala Problem Overview


Quick note: Examples from the tutorial Scala for Java Refugees Part 5: Traits and Types.

Suppose I have the traits Student, Worker, Underpaid, and Young.

How could I declare a class (not instance), CollegeStudent, with all these traits?

Note: I am aware of the simplests cases, such as CollegeStudent with one or two Traits:

class CollegeStudent extends Student with Worker

Scala Solutions


Solution 1 - Scala

It is easy, when declaring a class you just use the "with" keyword as often as you want

class CollegeStudent extends Student with Worker with Underpaid with Young

the order of the traits can be important if a trait is changing the behavior of the class, it all depends on traits you are using.

Also if you don't want to have a class which always uses the same traits you can use them later:

class CollegeStudent extends Student
new CollegeStudent with Worker with Underpaid with NotSoYoungAnymore

Solution 2 - Scala

I think that it is very important to explain not only the syntax, but also which role does the ordering of the traits play. I found the explanation in Jason Swartz's Learning Scala (page 177) quite enlightning.

  • A Scala class can extend multiple traits at once, but JVM classes can extend only one parent class. The Scala compiler solves this by creating "copies of each trait to form a tall, single-column hierarchy of the class and traits", a process known as linearization.

  • In this context, extending multiple traits with identical field names would fail to compile, exactly the same "as if you were extending a class and providing your own version of a method but failed to add an override keyword".

And since it determines the shape of the inheritance tree, the linearization order is indeed one very important question to regard. As an example, class D extends A with B with C (where A is a class and B and C are traits) would become class D extends C extends B extends A. The following few lines, also from the book, illustrate that perfectly:

trait Base { override def toString = "Base" }
class A extends Base { override def toString = "A->" + super.toString }
trait B extends Base { override def toString = "B->" + super.toString }
trait C extends Base { override def toString = "C->" + super.toString }
class D extends A with B with C { override def toString = "D->" + super.toString }

A call to new D() would have the REPL print the following:

 D->C->B->A->Base

Which perfectly reflects the structure of the linearized inheritance graph.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel RibeiroView Question on Stackoverflow
Solution 1 - ScalaAlexander StolzView Answer on Stackoverflow
Solution 2 - Scalafr_andresView Answer on Stackoverflow