Created
August 27, 2016 13:50
-
-
Save lpil/26088a555276bfbcd3ac0c7543aa8941 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
defmodule Pattern do | |
@pattern quote do: {:attr, payload} | |
defmacro pattern do | |
quote do: {:macro, payload} | |
end | |
def match(unquote(@pattern) = argument) do | |
IO.puts "Matched #{inspect argument} with module attr pattern" | |
end | |
def match(pattern() = argument) do | |
IO.puts "Matched #{inspect argument} with macro pattern" | |
end | |
def match(_) do | |
IO.puts "Didn't match anything" | |
end | |
end | |
Pattern.match 123 | |
# => Didn't match anything | |
Pattern.match {:macro, 456} | |
# => Matched {:macro, 456} with macro pattern | |
Pattern.match {:attr, 789} | |
# => Matched {:attr, 789} with module attr pattern |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment