# 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.

###### Previous episode
Structs đź¤ť Enums
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

### 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.

This episode is for subscribers only.

### Subscribe to Point-Free

Access this episode, plus all past and future episodes when you become a subscriber.

See plans and pricing

### 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

#### Algebraic Data Types

Brandon Williams & Stephen Celis â€˘ Monday Feb 19, 2018

Our introductory episode on algebraic data types. We introduce how structs and enums are like multiplication and addition, and we explore how this correspondence allows us to refactor our data types, simplify our code, and eliminate impossible states at compile time.

What does the Swift type system have to do with algebra? A lot! Weâ€™ll begin to explore this correspondence and see how it can help us create type-safe data structures that can catch runtime errors at compile time.

TypeScript has a feature closely related to enums and anonymous sum types. It is called â€śunion typesâ€ť, and it also allows you to express the idea of choosing between many different types.

#### TypeScript 2.0: Tagged Union Types

Marius Schulz â€˘ Thursday Nov 3, 2016

TypeScriptâ€™s type system is also flexible enough to emulateâ€™s Swiftâ€™s enums, but anonymously! Swift enums are sometimes called â€śvariantsâ€ť or â€śtagged unionsâ€ť because each case is â€śtaggedâ€ť with a label: for instance, the Optional type is tagged with some and none.

#### Null-tracking, or the difference between union and sum types

Waleed Khan â€˘ Monday Jul 24, 2017

An in-depth look at the differences between sum types and union types.

#### Polymorphic vs. ordinary variants in ocaml

Yehonathan Sharvit â€˘ Friday Mar 16, 2018

OCaml supports anonymous sum types in the form of â€śpolymorphic variants,â€ť which can describe a single, tagged case at a time.

#### Tuple : Struct :: ? : Enum

Matt Diephouse â€˘ Thursday Jun 22, 2017