As @NobbZ mentioned, the problem is that there’s no pattern matching inside a defguard
header.
I didn’t find good documentation on this, though. Only some discussion prior to implementing defguard
in v1.6:
defguard(h) is extremely limited in scope:
- the function head cannot pattern match
- code blocks are not allowed in guards
- no assignment
- no local functions
[1] https://github.com/elixir-lang/elixir/issues/2469
[2] Provide defguard · Issue #2469 · elixir-lang/elixir · GitHub
[3] Redirecting to Google Groups
[4] Defguard - experimenting with a structural matching alternative to Kernel.defguard
Follow up question: if I define a guard as a macro (according to the docs) then I can pattern match. However isn’t defguard
a syntactic sugar for this macro?