Training

Introduction to Swift Macros


This hands-on, one day introductory course is for experienced Swift developers who want to explore meta-programming with Swift Macros.

Contact us at inquiries@dimsumthinking.com to book or inquire about this course for your group, company, conference, or public event.

What you'll learn


Swift Macros gives you a lot of power to transform and customize existing Swift code. Apple takes advantage of it in the implementation of #Preview, @Observable, and @Model. We'll use these and others as inspiration to create our own Swift Macros.

We'll see an example that eliminates boilerplate code in the use of AsyncStreams. Throughout the day we look at various situations in which macros might and might not be appropriate to help you get a feel for when you should reach for this tool.

We examine the different types of macros that are available in Swift and have actual examples of why it's important to select the appropriate type. We'll consider what's involved in basic parsing before digging into parsing an Abstract Syntax Tree using SwiftSyntax to pull out the pieces you need for your macro. We put in error handling and write tests to make our macros more robust.

What you need


You need a Mac running the latest version of Xcode. You'll create projects throughout the day.

Syllabus Highlights


Part I - Getting Started

Exploring existing macros

The Macros Template

Passing Tests

Writing Tests

Part II - First steps in transforming code

Swift Syntax Builder

Parsing strings

Using ASTs

Reporting Issues

URL You build it

Part III - Using our macros

Using Macros

Macros in Macros

Macros Details

Part IV - Creating a variety of macros

Member Macro

Implementing a Member Macro

Conformance Macro

Multiple Conformance

Declaration Macro

Diagnostics