Macros Are Hygienic
Learn more about macros and their scope in Elixir.
We'll cover the following...
We'll cover the following...
Introduction
It’s 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:
Press + to interact
defmodule Scope dodefmacro update_local(val) dolocal = "some value"result = quote dolocal = unquote(val)IO.puts "End of macro body, local = #{local}"endIO.puts "In macro definition, local = #{local}"resultendenddefmodule Test dorequire Scopelocal = 123Scope.update_local("cat")IO.puts "On return, local = #{local}"end
Here’s the result of running that code:
In macro definition,
...