Chapter 8. Building scalable and extensible components

 

This chapter covers

  • Building components in Scala
  • A tour of various types of types in Scala
  • Ad hoc polymorphism with type classes
  • Solving expression problems in Scala

So far we’ve been working with Scala without paying any serious attention to its type system. The type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute.[1]

1 Benjamin C. Pierce, Types and Programming Languages, 2002, The MIT Press, www.cis.upenn.edu/~bcpierce/tapl/.

The challenge of learning about a type system is understanding the theory behind it. It’s always helpful to learn the fundamentals behind a good type system, but in this chapter my focus is on the practical benefits of a good type system without going too much into theory. In the process I explore various types of the types Scala provides you, with examples so you can understand their applications. Why is the type system so important? It provides the following features:

  • Error detectionThink of the compiler as a suite of test cases that can detect common type and other program errors.
  • AbstractionsThis is the focus of this chapter. You’ll learn how the type system provides abstractions to build components.
  • DocumentationThe signature of a function or method tells you a lot about what it’s doing.
  • EfficiencyThe type system helps the compiler generate optimized binary code.

8.1. Building your first component in Scala

8.2. Types of types in Scala

8.3. Ad hoc polymorphism with type classes

8.4. Summary