Skip to Content
Programming Scala, 3rd Edition
book

Programming Scala, 3rd Edition

by Dean Wampler
May 2021
Intermediate to advanced
553 pages
13h 45m
English
O'Reilly Media, Inc.
Book available
Content preview from Programming Scala, 3rd Edition

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.scala
package progscala3.traits.ui

abstract class ButtonWithCallbacks(val label: String,
    val callbacks: Seq[() => Unit] = Nil) extends Widget:            1

  def click(): Unit =                                                
    updateUI()
    callbacks.foreach(f => f())

  protected def updateUI
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Scala, 2nd Edition

Programming Scala, 2nd Edition

Dean Wampler, Alex Payne
Functional Programming in Scala, Second Edition

Functional Programming in Scala, Second Edition

Paul Chiusano, Runar Bjarnason, Michael Pilquist
The Go Programming Language

The Go Programming Language

Alan A. A. Donovan, Brian W. Kernighan

Publisher Resources

ISBN: 9781492077886Errata PageSupplemental Content