What def, val, and var fields in Scala traits look like after they’re compiled (including the classes that extend them)
I generally have a pretty good feel for how Scala traits work, and how they can be used for different needs. As one example, a few years ago I learned that it’s best to define abstract fields in traits using
def. But there are still a few things I wonder about.
Today I had a few free moments and I decided to look at what happens under the covers when you use
var fields in traits, and then mix-in or extend those traits with classes. So I created some examples, compiled them with
scalac -Xprint:all, and then decompiled them with JAD to see what everything looks like under the covers.
I was initially going to write a summary here, but if you want to know how things work under the hood, I think it helps to work through the examples, so for today I’ll leave that as an exercise for the reader.
As a brief note today, here’s an example of stackable modifications in Scala.
Lately I was curious about what
super means when you mix Scala traits into a class or object. A simplified answer is that
super refers to the last trait that’s mixed in, though I should be careful and note that this is an oversimplification.
This can be demonstrated in an example that uses both inheritance and mixins with traits. Given this combination of traits and classes:
As a quick note about traits in Scala, this StackOverflow page makes a few good points about sealed traits:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 8.8, “How to dynamically add a Scala trait to an object instance.”
Rather than add a trait to an entire class, you just want to add a trait to an object instance when the object is created.
Add the trait to the object when you construct it. This is demonstrated in a simple example:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 8.6, “How to mark a Scala trait so it can only be subclassed by a certain type.”
You want to mark your trait so it can only be used by types that extend a given base type.
To make sure a trait named
MyTrait can only be mixed into a class that is a subclass of a type named
BaseType, begin your trait with a
this: BaseType => declaration, as shown here:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.10, “How to handle constructor parameters when extending a Scala class.”
You want to extend a base Scala class, and need to work with the constructor parameters declared in the base class, as well as new parameters in the subclass.
This page contains a collection of Scala
trait examples. I created many of these examples when I was writing the Scala Cookbook. Unlike the Cookbook, where I explain these examples in great detail, on this page I’m just sharing many of the examples so you can use this as a
trait reference page. (The Cookbook actually contains more examples than this page.)
Without any more introduction, here are the examples.