mixing in

How to use Scala traits as simple mixins

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 8.4, “How to use Scala traits as simple mixins (or, How to mix in Scala traits).”

Problem

You want to design a solution where multiple traits can be mixed into a class to provide a robust design.

Solution

To implement a simple mixin, define the methods you want in your trait, then add the trait to your class using extends or with. For instance, the following code defines a Tail trait: