Skip to content

Instantly share code, notes, and snippets.

@lpil
Created August 27, 2016 13:50
Show Gist options
  • Save lpil/26088a555276bfbcd3ac0c7543aa8941 to your computer and use it in GitHub Desktop.
Save lpil/26088a555276bfbcd3ac0c7543aa8941 to your computer and use it in GitHub Desktop.
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