Skip to Content
Scala Cookbook, 2nd Edition
book

Scala Cookbook, 2nd Edition

by Alvin Alexander
August 2021
Intermediate to advanced
799 pages
17h 41m
English
O'Reilly Media, Inc.
Content preview from Scala Cookbook, 2nd Edition

Chapter 6. Traits and Enums

Because traits and enums are fundamental building blocks of large Scala applications, they’re covered here in this second domain modeling chapter.

Traits can be used to define granular units of behavior, and then those granular units can be combined to build larger components. As shown in Recipe 6.1, in their most basic use, they can be used like a pre–Java 8 interface, where the primary reason you use them is to declare the signatures for abstract methods that extending classes must implement.

However, Scala traits are much more powerful and flexible than this, and you can use them to define concrete methods and fields in addition to abstract members. Classes and objects can then mix in multiple traits. These features are demonstrated in Recipes 6.2, 6.3, and 6.4.

As a quick demonstration of this approach, rather than attempt to define everything a dog can do in a single Dog class, Scala lets you define traits for smaller units of functionality like a tail, legs, eyes, ears, nose, and a mouth. Those smaller units are easier to think about, create, test, and use, and they can later be combined together to create a complete dog:

class Dog extends Tail, Legs, Ears, Mouth, RubberyNose

That’s a very limited introduction to what Scala traits can do. Additional features include:

  • Abstract and concrete fields (Recipe 6.2)

  • Abstract and concrete methods (Recipe 6.3)

  • Classes that can mix in multiple traits, as shown in Recipes 6.4 and 6.5

  • The ability to ...

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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Scala Cookbook

Scala Cookbook

Alvin Alexander
Programming Scala, 2nd Edition

Programming Scala, 2nd Edition

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

Functional Programming in Scala, Second Edition

Runar Bjarnason, Michael Pilquist, Paul Chiusano

Publisher Resources

ISBN: 9781492051534Errata Page