The Case for Case Paths: Introduction

Episode #87 • Jan 20, 2020 • Subscriber-Only

You’ve heard of key paths, but…case paths!? Today we introduce the concept of “case paths,” a tool that helps you generically pick apart an enum just like key paths allow you to do for structs. It’s the tool you never knew you needed.

Collection
Case Paths
The Case for Case Paths: Introduction
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 explored the idea that Swift’s structs and enums are intimately and fundamentally related to each other in many past Point-Free episodes. We first did this in our episodes on algebraic data types, where we showed that in a very precise sense enums correspond to addition that we all know and love from algebra, and structs correspond to multiplication. We even showed that certain algebraic properties also carry over to Swift, like the fact that multiplication distributes over addition means that you can refactor your own data types to factor out common pieces of data.

We took this idea even further in another episode where we claimed that literally any feature you endow structs with there should be a corresponding version of it for enums, and vice versa. There are a few examples of where this principle holds, or mostly holds, for various features of structs and enums. However, in general this is not the case, and there are many instances where Swift clearly favors structs over enums where Swift gives structs many features that enums do not get.

One particular example we looked at was how ergonomic it is to access the data inside a struct. One can simply use dot syntax to get and set any field inside a struct value. The same is not true of enums, you have no choice but to do a switch, if case let or guard case let, which are syntactically heavy when all you wanna do is access a little bit of data inside an enum.

This led us down the path of defining what we called “enum properties”, which give enums the power of accessing their data with simple dot-syntax just like structs. We even created a code generation tool (part 1, part 2, part 3), using Apple’s Swift Syntax library, to automatically generate these properties for us to make it as easy as possible.

We want to take that work even further and discuss another feature that Swift structs are blessed with but which sadly has no representation for enums, and that is key paths. Key paths are a powerful feature in Swift, and we’ve talked about and used them a ton on Point-Free. Every field on a struct magically gets a key path generated for it by the compiler. But what does the corresponding concept look like when applied to enums? Answering this question will give us a tool for unlocking even more power from enums, and will even help us greatly simplify some code we previously wrote for the composable architecture.

Let’s start by reminding ourselves what key paths are and why they are so powerful.

Key paths: a refresher


References

  • Structs 🤝 Enums
    Brandon Williams & Stephen Celis • Mar 25, 2019

    In this episode we explore the duality of structs and enums and show that even though structs are typically endowed with features absent in enums, we can often recover these imbalances by exploring the corresponding notion.

    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.

  • Make your own code formatter in Swift
    Yasuhiro Inami • Jan 19, 2019

    Inami uses the concept of case paths (though he calls them prisms!) to demonstrate how to traverse and focus on various parts of a Swift syntax tree in order to rewrite it.

    Note

    Code formatter is one of the most important tool to write a beautiful Swift code. If you are working with the team, ‘code consistency’ is always a problem, and your team’s guideline and code review can probably ease a little. Since Xcode doesn’t fully fix our problems, now it’s a time to make our own automatic style-rule! In this talk, we will look into how Swift language forms a formal grammar and AST, how it can be parsed, and we will see the power of SwiftSyntax and it’s structured editing that everyone can practice.

  • Swift Tip: Bindings with KVO and Key Paths
    Chris Eidhof & Florian Kugler • Apr 24, 2018

    This handy Swift tip shows you how to create bindings between object values using key paths, similar to the helper we used in this episode.

  • Introduction to Optics: Lenses and Prisms
    Giulio Canti • Dec 8, 2016

    Swift’s key paths appear more generally in other languages in the form of “lenses”: a composable pair of getter/setter functions. Our case paths are correspondingly called “prisms”: a pair of functions that can attempt to extract a value, or embed it. In this article Giulio Canti introduces these concepts in JavaScript.

  • Optics By Example: Functional Lenses in Haskell
    Chris Penner

    Key paths and case paths are sometimes called lenses and prisms, but there are many more flavors of “optics” out there. Chris Penner explores many of them in this book.

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