Created
July 15, 2016 08:05
-
-
Save benjamintanweihao/4dbcce3d2bd188329f4c55a627c90f54 to your computer and use it in GitHub Desktop.
Dynamically generated macros using comprehension
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 Bar do | |
defmacro __using__(__opts) do | |
quote do | |
import unquote(__MODULE__) | |
end | |
end | |
for fn_name <- [:foo, :bar, :baz] do | |
defmacro unquote(fn_name)(do: inner) do | |
fn_name = unquote(fn_name) | |
quote do | |
IO.puts "#{unquote(fn_name)} called" | |
unquote(inner) | |
end | |
end | |
end | |
for fn_name <- [:qux, :quux, :corge] do | |
defmacro unquote(fn_name)() do | |
fn_name = unquote(fn_name) | |
quote do | |
IO.puts "#{unquote(fn_name)} called" | |
end | |
end | |
end | |
end | |
defmodule Foo do | |
use Bar | |
foo do | |
bar do | |
baz do | |
end | |
end | |
end | |
qux | |
quux | |
corge | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment