Why doesn't defmodule require unquote to use outside variables


I am wondering why it is not needed to unquote with defmodule when using variables that are defined outside of the call to defmodule.

For instance:

defmodule MacroMod do
  defmacro __using__(opts) do
    quote bind_quoted: binding() do
      dir = Keyword.fetch!(opts, :dir) |> dbg()
      def from_macro, do: unquote(dir)

dir = "/some/where"

defmodule UseMod do
  use MacroMod, dir: dir

  def direct, do: unquote(dir)

"/some/where" = UseMod.from_macro()
"/some/where" = UseMod.direct()

In the code above I can directly use the dir variable in use in UseMod, or usit it in a def (where there is an unquote but for the def scope).

Thank you.

1 Like