If you want to generate a dynamic list of arguments, you need to use unquote_splicing
, like this:
defmacro warp(name, argument_names, code) do
quote do
def unquote(name)(unquote_splicing(argument_names)) do
unquote(code)
end
end
end
Then later:
warp :foo, [a, b], {:ok, a, b}
which generates:
def foo(a, b), do: {:ok, a, b}
if you call that it will produce:
foo(1, 2)
# {:ok, 1, 2}
You can also define the macro like this without unquote_splicing
and just pass down the combined name and arguments to def
:
defmacro warp(name_and_args, do: code) do
quote do
def unquote(name_and_args) do
unquote(code)
end
end
end
This means you need to invoke warp
like you would invoke def
, for example:
warp foo(a, b), do: {:ok, a, b}