Last active
March 25, 2017 16:29
-
-
Save jc00ke/caef080fb8a40f14b092f24461914d27 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 Foo do | |
defmacro foo_macro(i) do | |
quote do | |
unquote(i) | |
end | |
end | |
def bar(foo_macro(1) = i), do: IO.puts(i) | |
def bar(foo_macro(2) = i) do | |
IO.puts "This better be 2: #{i}" | |
end | |
end | |
Foo.bar(1) | |
# => 1 | |
Foo.bar(2) | |
# => This better be 2: 2 | |
Foo.bar(3) | |
#** (FunctionClauseError) no function clause matching in Foo.bar/1 | |
# foo.exs:8: Foo.bar(3) | |
# (elixir) lib/code.ex:370: Code.require_file/2 |
@wobh yeah, that was my position, but others at the meeting didn't think it works work that way. This is a small POC to show them it actually worked.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. The macro-expansion happens before the function is defined, so there's no sense in which the macro is an "argument" to the function. During macro expansion it expands, in this case, into the values passed to it, so that when it's time to compile the function the only thing left is the binding:
1 = i
or2 = i
. As long as the code macros expand to is valid for the compiler, you can use macros anywhere. I hope this helps. If it doesn't, let me know and maybe I can help in some other way.