Skip to content

Instantly share code, notes, and snippets.

@blackode
Last active September 19, 2019 14:04
Show Gist options
  • Save blackode/7e1ceda22d145a5c6be400b2b9e36b56 to your computer and use it in GitHub Desktop.
Save blackode/7e1ceda22d145a5c6be400b2b9e36b56 to your computer and use it in GitHub Desktop.
Macros in guard clauses...
defmodule MyGuards do
defmacro is_kid age do
quote do: unquote(age) < 12
end
defmacro is_adult age do
quote do: unquote(age) > 18
end
end
# order of module matters .....
defmodule Hello do
import MyGuards
def hello(name,age) when is_kid(age) do
IO.puts "Hello Kid #{name}"
end
def hello(name,age) when is_adult(age) do
IO.puts "Hello Mister #{name}"
end
def hello(name,age) do
IO.puts "Hello Youth #{name}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment