Another Tour of Scala

ScalaTraits

The Gist

Traits and Mixin Class Composition cover the ways in which Scala models interfaces and abstract classes, allowing for multiple inheritance.

My Interpretation

Scala doesn’t have interfaces in the way that Java does; rather it uses Mixins (like Ruby), referred to as Traits.

A Trait is somewhat more similar to an abstract class in Java, except that in Scala, you can extend any number of traits that you wish.

Suppose our Circle class is getting refactored and enhanced. We wish to make the area method part of an interface that other shape classes might use. Further, we wish to be able to compare circles based on their size. In Java, we would implement the Comparable interface and a new interface called something like Area.

Now, further suppose that we want our comparison interface to be a bit richer than a simple compareTo method that returns an int; we want to clearly test for “less than”. In Java, we would need to extend the Comparable interface as so:

The problem is that the three new methods would have the exact same implementation in every case — they’d use the compareTo method. If our class already extends another class, we’re SOL. Traits let us combine the concepts:

There is a lot more that can be done with traits (and several no-doubt better implementations of the “super comparable” concept), but this should demonstrate what Traits can do and why they are useful.

My Thoughts on this Feature

I love mixins in Ruby and I love this feature. It does, however, make the scaladoc very hard to follow at times, and I just can’t for the life of me understand why you always extends the first trait. It would be so much nicer to only extend proper classes and instead with Traits; this would make reading code a lot easier.