0Pricing
Swift Academy · Lesson

Macro Roles: Freestanding vs Attached

Understanding #expression macros, @attached peer/member/accessor macro roles.

What Are Swift Macros?

Swift macros expand source code at compile time, generating boilerplate automatically without runtime overhead.

// Macros are invoked with # (freestanding) or @ (attached)
#warning("Fix this later")
@Observable class Counter { var count = 0 }

Freestanding Expression Macros

Freestanding expression macros start with # and produce a value or perform a side-effect.

// #stringify expands to a tuple of the value and its source string
let (val, src) = #stringify(1 + 2)
print(val, src) // 3, "1 + 2"

All lessons in this course

  1. Macro Roles: Freestanding vs Attached
  2. Writing a Freestanding Expression Macro
  3. Attached Member Macros for Boilerplate Reduction
  4. Testing and Debugging Macros
← Back to Swift Academy