🎉 End-of-year Sale! Save 25% when you subscribe today.

Macro Case Paths: Part 2

Episode #258 • Nov 20, 2023 • Subscriber-Only

We have now totally reimagined the design of our case paths library to create actual key paths for enum cases, but there is some boilerplate involved. Let’s create a macro that eliminates all of it and explore a few of the possibilities it unlocks.

Previous episode
Macro Case Paths: Part 2
Next episode
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

Brandon

So, we’ve done some pretty interesting things just now. We decided to house all of the case paths for an enum inside an inner Cases type, that way we don’t pollute the enum with a bunch of superfluous properties that will clutter autocomplete and the documentation of your types. And then we flipped the notion of “case path” on its head by instead thinking of it as a key path that locates a case path inside that Cases type. That instantly gave us key path syntax to procure case paths, and gave us type inference and nice autocomplete for free!

Stephen

But of course what we have done so far is not how anyone would want to write their applications. No one is going to define the Cases type for each one of their enums and then fill those types with properties for each case in the enum. That is a long, laborious process that is very easy to get wrong.

And this is a perfect use case for a macro. We should be able to annotate our enums with some kind of @CasePathable macro, and have all of that code generated for us automatically. This will be the first truly useful macro that we have built on Point-Free, and it is not a trivial one. There are some tricky aspects to it, and luckily we have our new MacroTesting library to help us each step of the way.

So, let’s see what it takes to write a macro to generate all of this case path boilerplate automatically.

Creating the macro


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