Chapter 10. Traits
Scala traits function as interfaces, abstract declarations of members (methods, fields, and types) that together express state and behavior. Traits can also provide concrete definitions (implementations) of some or all of those declarations.
A trait with concrete definitions works best when those definitions provide state and behavior that are well encapsulated and loosely coupled or even orthogonal to the rest of the state and behavior in the types that use the trait. The term mixin is used for such traits because we should be able to “mix together” such traits to compose different concrete types.
Traits as Mixins
We first discussed traits as mixins in “Traits: Interfaces and Mixins in Scala”, where we explored an example that mixes logging into a service. Logging is a good example of mixin behavior that can be well encapsulated and orthogonal to the state and behavior of the rest of a service.
Let’s revisit and expand on what we learned with a new example. First, consider the following code for a button in a GUI toolkit, which uses callbacks to notify clients when clicks occur:
// src/main/scala/progscala3/traits/ui/ButtonCallbacks.scalapackageprogscala3.traits.uiabstractclassButtonWithCallbacks(vallabel:String,valcallbacks:Seq[()=>Unit]=Nil)extendsWidget:defclick():Unit=updateUI()callbacks.foreach(f=>f())protecteddefupdateUI