A video series exploring functional programming and Swift.
#53 • Monday Apr 8, 2019 • Subscriber-only

Swift Syntax Enum Properties

We’ve seen how “enum properties” help close the gap between the ergonomics of accessing data on structs and enums, but defining them by hand requires a lot of boilerplate. This week we join forces with Apple’s Swift Syntax library to generate this boilerplate automatically!

This episode builds on concepts introduced previously:

#53 • Monday Apr 8, 2019 • Subscriber-only

Swift Syntax Enum Properties

We’ve seen how “enum properties” help close the gap between the ergonomics of accessing data on structs and enums, but defining them by hand requires a lot of boilerplate. This week we join forces with Apple’s Swift Syntax library to generate this boilerplate automatically!

This episode builds on concepts introduced previously:


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’ve now compared struct property access to enum associated value access and seen how structs are much more ergonomic by default, but we’ve also seen that we can recover these ergonomics on enums by defining our own computed properties per case. Unfortunately it comes at the cost of manual intervention: where the compiler gives us struct properties and key paths for free, we’re responsible for defining enum properties by hand, so it’s easy for us to forget to do so, and we can easily end up in a situation where some enum cases have properties defined while others don’t.

In order to embrace the idea of enum properties and benefit from their ergonomics universally without having to remember to take the time to define them by hand, we can turn to code generation. The Swift community has a bunch of tools that help here. The standard library team uses gyb, which stands for “generate your boilerplate”. It uses Python to interpolate templates and generate a bunch of standard library boilerplate. Another popular community tool is Sourcery. Today we’re going to use a relatively new tool from Apple called SwiftSyntax, which is a Swift language wrapper around libSyntax, a library for parsing and inspecting Swift source code.

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. One problem with our enum property code generator is that it only handles enum cases with a single associated value. Update it to handle enum cases with no associated values. What type of value does this enum property return?

  2. Update our code generator to handle enum cases with several associated values.

  3. Update our code generator to handle enum cases with labeled associated values. For example, we defined a Node enum in our episode on building a Swift HTML DSL:

    enum Node {
      case el(tag: String, attributes: [String: String], children: [Node])
      case text(String)
    }
    

    How might labels enhance our enum properties?

  4. After you add support for labeled enum cases, ensure that the code generator properly handles enum cases with a single, labeled value.

Chapters
Introduction
00:05
Swift Package Manager
01:35
Swift Syntax
08:36
Generating properties
14:41
A finishing touch for valid code
20:00
Till next time
22:34