🎉 End-of-year Sale! Save 25% when you subscribe today.

Structs 🤝 Enums

Episode #51 • Mar 25, 2019 • Subscriber-Only

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.

Collection
Enum Properties
Structs 🤝 Enums
Locked

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Sign in with GitHub

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.

An algebraic data types refresher


References

Downloads

Get started with our free plan

Our free plan includes 1 subscriber-only episode of your choice, access to 64 free episodes with transcripts and code samples, and weekly updates from our newsletter.

View plans and pricing