A macro is Elixir code that runs at compile time, receives parsed source code, and returns an alternative version. This transforms program structure before execution rather than operating on runtime values. section 2.6
Elixir macros operate on an abstract syntax tree rather than C-style textual substitution. That structure supports transformations over arbitrarily complex, nested expressions and enables language extensions that resemble built-in constructs. section 1.2.1 section 2.6
Use macros to:
- Reduce repetitive code and boilerplate by generating alternative code during compilation. section 2.6
- Build expressive mini-DSLs and language constructs on top of a small core. section 2.6
- Extend existing abstractions without hiding the runtime mechanisms that matter for production behavior. section 1.2.1
Transforming unless
The unless construct can be expressed as a macro that transforms `unless some_expression do block_1 else block_2 end` into an equivalent `if` expression with the branches reversed. The syntax looks built in, but the transformation occurs during compilation. section 2.6
Injecting GenServer callbacks
Writing `use GenServer` invokes a macro during compilation. The macro injects default callback functions into the calling module; defining a callback with the same name and arity overrides that default. section 6.2.2
unless some_expression do block_1 else block_2 end
Compare the macro-shaped `unless` form with its equivalent `if` expansion. section 2.6








