Last active
September 19, 2019 14:04
-
-
Save blackode/7e1ceda22d145a5c6be400b2b9e36b56 to your computer and use it in GitHub Desktop.
Macros in guard clauses...
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 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