Using unquote outside of quote block

Because defmodule does not call Macro.escape(..., unquote: true) and therefor unquote fragments are not supported. The reason is because unquote fragments are not needed for defmodule since the body of the module is evaluated when the surrounding code is evaluated, unlike functions where the body is evaluated only after the module is compiled.

You don’t need unquote fragments because this works:

iex(1)> x = 21
21
iex(2)> defmodule Foo do x + x end
{:module, Foo,
 <<70, 79, 82, 49, 0, 0, 3, 200, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 94,
   131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115,
   95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>, 42}
4 Likes