Last active
August 29, 2015 14:00
-
-
Save alco/11254250 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MacroM do | |
defmacro h_transform(mod) do | |
IO.puts "Applying h_transform" | |
IO.inspect(mod) | |
end | |
defmacro la_transform(mod) do | |
IO.puts "Applying la_transform" | |
IO.inspect(mod) | |
end | |
defmacro defaugmod(head, body) do | |
{ | |
{:., _, [Kernel, :access]}, | |
_, | |
[ | |
{:__aliases__, _, [modname]}, | |
transformers | |
] | |
} = head | |
quote do | |
defmodule unquote(Module.concat([modname])) do | |
unquote(body) | |
end |> unquote(build_pipe(transformers)) | |
end | |
end | |
defp build_pipe([h]) do | |
h | |
end | |
defp build_pipe([h|t]) do | |
{:|>, [], [h, build_pipe(t)]} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $ elixir -r modaug_macro.ex modaug_test.ex | |
import MacroM | |
defaugmod LAXTester[h_transform, la_transform] do | |
def hello do | |
"Hello" | |
end | |
end | |
IO.puts LAXTester.hello |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment