Chapter 23. Types
As you can tell from one look at the Scaladoc for the collections classes, Scala has a powerful type system. However, unless you’re the creator of a library, you can go a long way in Scala without having to go too far down into the depths of Scala types. But once you start creating libraries for other users, you will need to learn them.
This chapter provides recipes for the most common type-related problems you’ll encounter, but when you need to go deeper, I highly recommend the book Programming in Scala (Artima). Martin Odersky, one of its authors, is the creator of the Scala programming language, and I think of that book as “the reference” for Scala.
Scala’s type system uses a set of symbols to express different generic type concepts, including the concepts of bounds, variance, and constraints. Before jumping into the recipes, the most common of these symbols are summarized in the following sections.
A Note About Programming Levels and Types
Way back in January 2011, Martin Odersky defined six levels of knowledge that are needed for different types of Scala programmers. He uses the levels A1-A3 for application programmers, and L1-L3 for library designers. The type-related techniques that are demonstrated in this chapter correspond to his levels L1 through L3.
Generic Type Parameters
When you first begin writing Scala code, you’ll use types like Int
, String
, and custom types you create, like Person
, Employee
, and Pizza
. Then you’ll create traits, classes, ...
Get Scala Cookbook, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.