Skip to content

Instantly share code, notes, and snippets.

@zambal
Created March 28, 2015 10:20
Show Gist options
  • Save zambal/b83c198fb4d58186c733 to your computer and use it in GitHub Desktop.
Save zambal/b83c198fb4d58186c733 to your computer and use it in GitHub Desktop.
Binding issue with eval_quoted and quote/unquote
defmodule P do
defmacro precompile(call, block) do
{ expr, _ } = Code.eval_quoted(block[:do], [], __CALLER__)
quote do
def unquote(call) do
unquote(expr)
end
end
end
end
defmodule Mod do
defmacro square(n) do
quote do
n = unquote(n)
n * n
end
end
end
defmodule Test do
import P
require Mod
defmacro another_square(n) do
quote do
n = unquote(n)
n * n
end
end
precompile working do
quoted = quote do
x = 12
Mod.square(x)
end
[2 * 3, quoted]
end
precompile not_working1 do
quoted = quote do
x = 12
another_square(x)
end
[2 * 3, quoted]
end
precompile not_working2 do
quoted = quote do
x = 12
Test.another_square(x)
end
[2 * 3, quoted]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment