Last active
August 29, 2015 14:04
-
-
Save patrickgombert/02ba11fa2e4e24da4aab to your computer and use it in GitHub Desktop.
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
defmacro defguard({name, _, args}, [do: body]) do | |
quote bind_quoted: [args: args, body: body, name: name] do | |
defmacro unquote(name)(unquote_splicing(args)) do | |
if Macro.Env.in_guard?(__CALLER__) do | |
quote do | |
unquote(body) | |
end | |
else | |
eager_bindings = Enum.map args, fn({name, _, _}) -> | |
{:=, [], [{name, [], nil}, name]} | |
end | |
quote bind_quoted: [eager_bindings: eager_bindings, body: body], unquote: true, do: unquote(body) | |
end | |
end | |
end | |
end | |
defguard foo(bar), do: bar == "bar" | |
# => (CompileError) undefined function bar/0 |
josevalim
commented
Aug 1, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment