Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created April 6, 2017 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myronmarston/f8cf2872351fb28e9d59cee9b8cd98a4 to your computer and use it in GitHub Desktop.
Save myronmarston/f8cf2872351fb28e9d59cee9b8cd98a4 to your computer and use it in GitHub Desktop.
defmodule MyMacros do
defmacro my_macro() do
module = __CALLER__.module
Module.get_attribute(module, :foo) |> IO.inspect(label: "outside quote")
quote do
Module.get_attribute(unquote(module), :foo) |> IO.inspect(label: "with unquoted module")
Module.get_attribute(__MODULE__, :foo) |> IO.inspect(label: "with __MODULE__")
end
end
end
defmodule UseMacros do
require MyMacros
@foo bar: 1
MyMacros.my_macro()
end
outside quote: nil
with unquoted module: [bar: 1]
with __MODULE__: [bar: 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment