A new Swift video series exploring functional programming and more.
#51 • Monday Mar 25, 2019 • Subscriber-only

Structs 🤝 Enums

Name a more iconic duo… We’ll wait. Structs and enums go together like peanut butter and jelly, or multiplication and addition. One’s no more important than the other: they’re completely complementary. This week we’ll explore how features on one may surprisingly manifest themselves on the other.

#51 • Monday Mar 25, 2019 • Subscriber-only

Structs 🤝 Enums

Name a more iconic duo… We’ll wait. Structs and enums go together like peanut butter and jelly, or multiplication and addition. One’s no more important than the other: they’re completely complementary. This week we’ll explore how features on one may surprisingly manifest themselves on the other.


Subscribe to Point‑Free

This episode is for subscribers only. To access it, and all past and future episodes, become a subscriber today!

See subscription optionsorLog in

Sign up for our weekly newsletter to be notified of new episodes, and unlock access to any subscriber-only episode of your choosing!

Sign up for free episode

Introduction

We have talked about algebraic data types a number of times on this series, and we feel it helps wipe away some of the complexity in the language because we can see the simple math that lies underneath. For example, we saw that structs in Swift behave a lot like multiplication does from algebra, and that enums behave a lot like addition. And knowing that we could refactor our data types to simplify them without changing their intrinsic structure, or we could refactor them to remove invalid states.

We want to take this idea a step further. We want to say that the connection between Swift’s data types and algebra’s operations is so foundational that we should be emboldened to transfer concepts that are currently defined only on structs over to enums, and vice versa. This will help us see that there are a lot of lots of helpful operations on enums that Swift could be providing to us, but isn’t yet. But maybe someday!

So, let’s get started. We want to begin with a quick refresher on algebraic data types. We still highly recommend people watch our previous episodes (part 1 🆓, part 2, part 3) to get a serious, in-depth look, but this refresher will at least make sure we are all on the same page when it comes to algebraic data types.

Subscribe to Point-Free

👋 Hey there! Does this episode sound interesting? Well, then you may want to subscribe so that you get access to this episodes and more!


Exercises

  1. A nice feature that Swift structs have is “properties.” You can access the fields inside the struct via dot syntax, for example view.frame.origin.x. Enums don’t have this feature, but try to explain what the equivalent feature would be.

  2. Swift enums are sometimes called “tagged unions” because each case is “tagged” with a name. For instance, Optional tags its wrapped value with the some case, and tags the absence of a value with none. This is in contrast to “union” types, which merely describe the idea of choosing between many types, e.g. an optional string can be expressed as string | void, and a number or a string can be expressed as number | string.

    What can an tagged union do that a union can’t do? How might string | void | void be evaluated as a union vs. how might it be represented as an enum?

  3. The Either type we use in this episode is the closest to an anonymous sum type that we get in Swift as it has no semantics, but it only supports two cases. What are some ways of supporting an “anonymous” sum of three cases? What about four? Or more? Is it possible to support three or more cases using just the Either type?


References

Chapters
Introduction
00:05
An algebraic data types refresher
01:44
Initializers
09:53
Exhaustive compiler checks
12:36
Anonymous types
17:09
What’s the point?
29:14