The hygienic evaluation of quotes
In Elixir, quoted literals are evaluated hygienically. This means variables defined outside the scope of the quoted literals do not get defined inside the quoted literal scope. It also means that the variables defined inside the quoted literals do not get defined outside the scope, even upon evaluation.
Let’s take a look at the following code:
iex shell iex> expr = quote do: b = 2 {:=, [], [{:b, [], Elixir}, 2]} iex> Code.eval_quoted(expr) {2, [{{:b, Elixir}, 2}]} iex> b ** (CompileError) iex: undefined function b/0
As you can see in the preceding code, anything defined inside a quoted literal upon its evaluation isn’t defined outside its scope. Even though the b
variable was defined inside the scope of the expression when it was evaluated, trying to reference it outside that scope raised an error.
Now, let’s look at the following code snippet:
iex shell iex> a = 1 1 iex> expr = quote do: 1 + a {:...