Chapter 3. Composition

This chapter covers

  • Combining types into compound types
  • Combining types as either-or types
  • Implementing visitor patterns
  • Algebraic data types

In chapter 2, we looked at some common primitive types that form the building blocks of a type system. In this chapter, we’ll look at ways to combine them to define new types.

We’ll cover compound types, which group values of several types. We’ll look at how naming members gives meaning to data and lowers the chance of misinterpretation, and how we can ensure that values are well-formed when they need to meet certain constraints.

Next, we’ll go over either-or types, which contain a single value from one of several types. We will look at some common types such as optional types, ...

Get Programming with Types 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.