Composition of Types

You’ll hear the word “composition” used a lot in functional programming—it’s the foundation of functional design. Composition just means that you can combine two things to make a bigger thing, like using Lego blocks.

In the functional programming world, we use composition to build new functions from smaller functions and new types from smaller types. We’ll talk about composition of types right now, and we’ll talk about function composition later, in Chapter 8, Understanding Functions.

In F#, new types are built from smaller types in two ways:

  • By _AND_ing them together
  • By _OR_ing them together

“AND” Types

Let’s start with building types using AND. For example, we might say that to make fruit salad you need an apple and a ...

Get Domain Modeling Made Functional 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.