Skip to content

Last modified: April 30, 2025

Macros

macros: the ability to write a source-level construct


Two kinds of macros

syntactic macros (C, C++ preprocessor)
    simple text drop-in replacement
    little to no compiler support
    potentially itself a source of bugs

semantic macros (Lisp, Scheme, etc)
    fully checked and understood by the compiler
    verified correct
    this is Swift's implementation


Swift macro types

freestanding macros
    #-prefixed
    appear anywhere in code
    almost like source-level function calls

attached macros
    @-prefixed
    modify a declaration they're "attached" to
    resemble other languages' "custom attributes" or "annotations"

Freestanding macros: "#function" and "#warning"

func myFunction() {
    print("Currently running \(#function)")
    #warning("Something's wrong")
}

#function returns the name of the currently-scoped function
#warning issues a warning during compilation

An attached macro: "OptionSet"

@OptionSet<Int>
struct SundaeToppings {
    private enum Options: Int {
        case nuts
        case cherry
        case fudge
    }
}

This macro sets "nuts", "cherry", and "fudge" to be "bit flags" values (1, 2, 4, ...)