There are lots of patterns which developers use extensively in Scala world. Some of the famous ones are stackable traits pattern, cake pattern and magnet pattern. They are very common to be found in any good Scala projects, you might not understand them in one read, it takes some good amount of time and patience to understand the beauty of each one of them. Today we will check out traits in scala and what is so cool about stackable traits pattern.

Traits are exactly what it means in natural language. Let’s begin with an example.

The example is based on characters of Game of Thrones, to make understanding concepts less boring. Don’t just understand the story but also give your attention to the functionality of traits.

Here are few things you should have noticed:

Traits are like interfaces but can have concrete methods. How easily we can implement multiple inheritance in Scala. How does the compiler know whose trait to consider in case of Joffrey and Tommen, i.e. Cersei or Jamie? That works on the principle of linearization.

This is how linearization works:

Lannister → AnyRef → Any

Tywin → Lannister → AnyRef → Any

Jamie → Tywin → Lannister → AnyRef → Any

Cersei → Tywin → Lannister → AnyRef → Any

Joffrey → Cersei → Jamie → Tywin → Lannister → AnyRef → Any

Observe it happens from top to bottom, and each class comes only once.

Observer the last case, Tywin is the parent of Cersei, but as it is already in the linearization of Jamie, it is not added again.

Quoting The Design Pattern Thesis

An important aspect of the linearization process is that it is fairly easy to understand the mechanism when programming. There are basically two things to remember: The overriding strategy and that in any linearization, a class is always linearized before all of its superclasses and mixed in traits.

So in our case, the compiler first tries to find payTheirDebts method in Joffrey, doesn’t find it, then it needs to figure out whose payTheirDebts should be called. Here the order or traits, which care also called mixins matters and makes all the difference.

The basic rule is that the right most trait takes the foremost effect, therefore we see the results different for Joffrey and Tommen as their mixin order is different. Joffrey gets payTheirDebts trait from Cersei while Tommen gets it from Jamie.

But, but the above basic rule has a second part to it which makes it more interesting.

The right most trait takes the foremost effect, if the implementation of that method calls super then it will call the next one in the linear hierarchy i.e. moving left.

Time to understand the above behavior using the same example with some more complications.

Things to observe:

Joffrey has the method, it is directly called, no tracking back to parents. Tommen doesn’t have the method, it follows the linearization hierarchy, i.e. Jamie will double the amount to 200, Cersei will reduce the amount by 1/4 making it 50 and Tywin will return 200+50 i.e. amount+debt. Leon is another interesting case, which might confuse you. Thing which you might have missed is Tyrion is not calling his parent, hence Tywin’s amount + debt is not being returned, only the amount from Tyrion’s implementation is being returned as the final value of the method. This behavior of stacking of traits one after another is known as a pattern Stackable Traits.

Now let me take you to the final interesting part of the story.

The story goes on like this. Shae comes into the scene, and claims she has always been a Lannister and is a bastard of Tywin Lannister. She also claims that Leon is her and Tyrion’s child. As Leon has been anonymous till now, we will create it as an anonymous instance. Tyrion didn’t know how to react when he heard this story from Shae, therefore, he killed Shae and Tywin both the night he eloped the prison in Kings Landing.

Things to observe in the code above:

Tywin doesn’t have an implementation of addToDebt method, so Tyrion can’t call super directly as the method is abstract. The compiler allows that only if the override is abstract. If you didn’t observe there is Tyrion abstract overrides that method. Abstract override will take the implementation of any concrete class which extends from Tywin and has traits of Tyrion. In our case, it turns out to be the son of Shae and Tyrion, Leon.

Thanks for reading. I hope you enjoyed as well as got some understanding of Stackable trait pattern in Scala.

Here are some more awesome resources.

2. http://www.scala-lang.org/old/sites/default/files/FrederikThesis.pdf

3. https://www.safaribooksonline.com/blog/2013/05/30/traits-how-scala-tames-multiple-inheritance/