Macros
- Macros. A macro receives code as data and returns code: Elixir extending itself at compile time.
- AST. Elixir code is quotable AST: three-element tuples the macro system rewrites.
- Macros in the wild. The ecosystem’s elegance is macros: routers, schemas and tests are macro-built DSLs.
Quote and Unquote
- quote. quote captures code as AST: the raw material macros transform.
- unquote. unquote splices values into quoted code: templates filled at compile time.
- Hygiene. Elixir macros are hygienic: their variables cannot silently capture yours.
Compile-Time Extension
- use and __using__. use invokes a module’s __using__ macro: the injection point behind every framework DSL.
- Compile-time checks. Macros can validate at compile time: wrong configuration failing the build, not the runtime.
- Macro restraint. The first rule of macros: don’t: reach for them when functions provably cannot.
Before and after this level
- Before: Level 9: Mix & Hex: Projects, Dependencies, Packages and Libraries.
- Next: Level 11: Distributed Elixir: Nodes, Clustering, Distributed Failure.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does Elixir Level 10: Metaprogramming cover?
Macros, Quote and Unquote, Compile-Time Extension: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 9: Mix & Hex, which covers Projects, Dependencies, Packages and Libraries.