Skip to content

Instantly share code, notes, and snippets.

@nanne007
Last active July 27, 2016 12:03
Show Gist options
  • Save nanne007/9a271ba31b6771f4563f645fae9b9815 to your computer and use it in GitHub Desktop.
Save nanne007/9a271ba31b6771f4563f645fae9b9815 to your computer and use it in GitHub Desktop.
strange behavior of elixir macro
defmodule FuncMacro do
defmacro func(name) do
quote do
def unquote(name)(args) do
Enum.join(args, "-")
end
end
end
defmacro func_with_body(name, do: body) do
quote do
def unquote(name)(args) do
unquote(body)
end
end
end
end
defmodule TestModule do
require FuncMacro
# This can work correctly.
FuncMacro.func :func_without_body
# Why this not?
FuncMacro.func_with_body :func_with_body do
Enum.join(args, "-")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment