sensible face on it. It gives you all the power of Erlang plus a powerful macro system." — Dave Thomas, author of Programming Elixir, co-author of Pragmatic Programmer
Iiiiitt deeeepeeeeeends — Best answer: Eeeh, maybe, depends on who you ask — Not the Ruby kind of meta-programming — Macros really work with code: not with objects/ classes/methods/functions/etc.
data structures — Just like a JSON parser turns a string into objects, arrays, integers, strings, ... — Parsers turn code strings into syntax trees (or Abstract Syntax Trees)
"has this identifier been defined before?" — "is this code unused?" — Modify it — "Let's remove the commented-out code" — "Let's replace calls to this function with the body of the function itself" — Pass it around — "lets hand this and the code from the other files to the pretty printer"
— Parse and modify Ruby in C — Parser in C, AST in C, code that uses AST in C — Elixir — "Code is data" — Implemented in Erlang (doesn't matter) — You can parse and modify Elixir in Elixir — Parse in Elixir, AST in Elixir, code that uses AST in Elixir
example for macros - in any language! Should look like this: unless 5 == 3, do: IO.inspect("will be printed") unless 3 == 3, do: IO.inspect("will not be printed")
— Macro Expansion: replace calls to macros in the source code with the result of the call — Parsing -> Macro Expansion Phase -> Compilation/ Interpretation — Macros receive AST nodes and return AST nodes