Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created April 6, 2017 17:36
Show Gist options
  • Select an option

  • Save myronmarston/f8cf2872351fb28e9d59cee9b8cd98a4 to your computer and use it in GitHub Desktop.

Select an option

Save myronmarston/f8cf2872351fb28e9d59cee9b8cd98a4 to your computer and use it in GitHub Desktop.

Revisions

  1. myronmarston created this gist Apr 6, 2017.
    18 changes: 18 additions & 0 deletions my_macros.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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
    3 changes: 3 additions & 0 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    outside quote: nil
    with unquoted module: [bar: 1]
    with __MODULE__: [bar: 1]