Macros Are Hygienic

It is tempting to think of macros as some kind of textual substitution—a macro’s body is expanded as text and then compiled at the point of call. But that’s not the case. Consider this example:

macros/hygiene.ex
 defmodule​ Scope ​do
 defmacro​ update_local(val) ​do
  local = ​"​​some value"
  result = ​quote​ ​do
  local = ​unquote​(val)
  IO.puts ​"​​End of macro body, local = ​​#{​local​}​​"
 end
  IO.puts ​"​​In macro definition, local = ​​#{​local​}​​"
  result
 end
 end
 defmodule​ Test ​do
 require​ Scope
 
  local = 123
  Scope.update_local(​"​​cat"​)
  IO.puts ​"​​On return, local = ​​#{​local​}​​"
 end

Here’s the result of running that code:

 In macro definition, local = some value
 End of macro body, local = cat
 On return, local = 123

If the macro body were just substituted in at the point of call, both it and the module Test would share the same scope, and the macro would overwrite the variable local, so we’d see

 In macro definition, local = some value
 End of macro body, local = cat
 On return, local = cat

But that isn’t what happens. Instead, the macro definition has both its own scope and a scope during execution of the quoted macro body. Both are distinct from the scope within the Test module. The upshot is that macros will not clobber each other’s variables or the variables of modules and functions that use them.

The import and alias functions are also locally scoped. See the documentation for quote for a full description. This also describes how to turn off hygiene for variables and how to control the stack trace’s format if things go wrong while executing a macro.