Last active
July 27, 2016 12:03
-
-
Save nanne007/9a271ba31b6771f4563f645fae9b9815 to your computer and use it in GitHub Desktop.
strange behavior of elixir macro
This file contains hidden or 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 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