🎉 Black Friday Sale! Save 30% when you subscribe today.

The Case for Case Paths: Properties

Episode #88 • Jan 27, 2020 • Subscriber-Only

We’ve now seen that it’s possible to define “case paths”: the enum equivalent of key paths. So what are their features? Let’s explore a few properties of key paths to see if there are corresponding concepts on case paths.

Collection
Case Paths
The Case for Case Paths: Properties
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

So creating a case path is quite easy, but there are a lot of things not completely right with this code snippet. First, it is not right to pollute the Result namespace with these static vars, it would be better to have a different place to store them. Also, there is some boilerplate involved in creating these case paths. We see it here with this if case let stuff when implementing the extract, and basically every case path we create will look exactly like this, and after awhile it’s going to be a pain to have to write over and over.

We are going to solve both of those problems soon, but first we want to explore some of the properties of case paths and show the similarities with key paths. There are some operations that the Swift standard library provides for key paths, and we of course would expect that there is some version of those operations for case paths too.

Appending paths


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.

  • Getters and Key Paths
    Brandon Williams & Stephen Celis • Mar 19, 2018

    In this episode we first define the ^ operator to lift key paths to getter functions.

    Note

    Key paths aren’t just for setting. They also assist in getting values inside nested structures in a composable way. This can be powerful, allowing us to make the Swift standard library more expressive with no boilerplate.

  • SE-0249: Key Path Expressions as Functions
    Stephen Celis & Greg Titus • Mar 19, 2019

    A proposal has been accepted in the Swift evolution process that would allow key paths to be automatically promoted to getter functions. This would allow using key paths in much the same way you would use functions, but perhaps more succinctly: users.map(\.name).

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

  • How Do I Write If Case Let in Swift?
    Zoë Smith

    This site is a cheat sheet for if case let syntax in Swift, which can be seriously complicated.

  • 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