Macro Roles: Freestanding vs Attached
Understanding #expression macros, @attached peer/member/accessor macro roles.
Macro Roles: Freestanding vs Attached is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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"Freestanding Declaration Macros
Freestanding declaration macros emit new declarations (e.g., functions, variables) at the usage site.
// #makePair emits two variables
#makePair("point", x: 1, y: 2)
// expands to: let pointX = 1; let pointY = 2Attached Member Macros
Attached @member macros add new stored/computed properties, init, or methods to the type they are attached to.
@AddID
struct User { var name: String }
// Expands to: var id = UUID()Attached Accessor Macros
Attached @accessor macros generate getter/setter implementations for stored properties.
struct Point {
@Clamped(0...100)
var x: Int = 0
// accessor macro generates get/set with clamping
}Attached Peer Macros
Peer macros add sibling declarations next to the declaration they are attached to.
class Service {
@AddAsync
func fetchSync(completion: @escaping (Data)->Void) { ... }
// peer macro adds: func fetch() async -> Data
}Attached Conformance Macros
Conformance macros add protocol conformances and the required implementations to a type.
@EquatableByID
struct Product { var id: Int; var name: String }
// adds: extension Product: Equatable { ... }Attached Extension Macros
Extension macros generate entire extension blocks for a type, useful for adding complex protocol implementations.
@CustomCodable
struct Config { var host: String; var port: Int }
// adds: extension Config: Codable { custom encode/decode }Macro Expansion is Compile-Time
All macro expansions happen during compilation — no runtime cost and the expanded code is visible in Xcode.
// Right-click macro usage in Xcode → "Expand Macro"
// to see the generated source codeDeclaring a Macro
Macros are declared in a separate macro target using the @freestanding or @attached attribute.
@freestanding(expression)
public macro stringify<T>(_ value: T) -> (T, String)
= #externalMacro(module: "MyMacros", type: "StringifyMacro")Macro Roles Summary
Freestanding: #expression, #declaration. Attached: @member, @accessor, @peer, @conformance, @extension.
// Freestanding
let result = #stringify(40 + 2)
// Attached
@Observable class Model { var count = 0 }Quick Check
Which macro role adds new members (like properties or init) to the type it decorates?
Lesson Recap
Freestanding macros (#name) produce values or declarations. Attached macros (@name) augment types with members, accessors, peers, conformances or extensions. All expansion happens at compile time.
Frequently asked questions
Is the “Macro Roles: Freestanding vs Attached” lesson free?
Yes — the full text of “Macro Roles: Freestanding vs Attached” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Macro Roles: Freestanding vs Attached”?
Understanding #expression macros, @attached peer/member/accessor macro roles. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Macro Roles: Freestanding vs Attached” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Macro Roles: Freestanding vs Attached
- Writing a Freestanding Expression Macro
- Attached Member Macros for Boilerplate Reduction
- Testing and Debugging Macros