Swift Macros Deep Dive (Part 2)
Write Your Own!
The second part of this Swift Macros Deep Dive covers the process of writing, testing, and debugging Swift macros in detail.
In the video, I solve three different tasks in BirdChat (my demo project) with the help of three different flavors of macros:
- a declaration (freestanding) macro allows me to radically reduce the amount of boilerplate when declaring ready-to-present enums—the declarations now take just one line;
- an attached macro helps with conforming types to the
Equatable
protocol in a custom way—but without writing much code; - and an expression (freestanding) macro makes it much easier to keep track of what still needs to be done in the project—and implement the missing pieces when I’m ready.
Besides, I demonstrate how you start from scratch and create a macros package, and how to debug and test the macros to make sure they work just as expected.
Also watch the first part where I explain why macros were added to Swift, how they work, how their flavors differ from each other, and—most importantly—how you can use them to make your code shorter and safer.
Links
- The BirdChat app for you to practice (includes branches with a starter template and complete project)
- Macros documentation
- Swift Syntax
- Attached macros’ attributes
- Applying macros
- Freestanding (expression) macros
- Freestanding (declaration) macros
- Attached macros
- Macros examples
- Curated collection of Swift macros