Swift Syntax Enum Properties

Episode #53 • Apr 8, 2019 • Subscriber-Only

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!

Previous episode
Swift Syntax Enum Properties
Swift Package Manager
Swift Syntax
Generating properties
A finishing touch for valid code
Till next time

Unlock This Episode

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


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.

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

Already a subscriber? Log in


  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.