9 Algebraic data types and pattern matching

This chapter covers

  • Removing invalid states from programs
  • Using algebraic data types
  • Handling errors with optional values and variants
  • Creating overloaded function objects
  • Handling algebraic data types through pattern matching

During the course of this book, we’ve tackled a few problems the program state introduces. You’ve seen how to design software without mutable state and how to implement data structures that are efficient to copy. But we still haven’t dealt with all the problems of program state: we haven’t covered unexpected or invalid states.

Consider the following situation. Suppose you have an application that counts the total number of words on a web page. The program can start counting ...

Get Functional Programming in C++ now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.