Delegate pattern matching to function

Hey,
I’m fairly new to macros world and I have a problem with implementing quite simple macro:

defmacro matches_pattern?({f, args}, pattern) do
    quote bind_quoted: [f: f, args: args, pattern: pattern] do
      Enum.all?(args, fn x -> x match?(f.(x), pattern) end)
    end
  end

It is supposed to apply function on all arguments and check if all of those function results matches some pattern.
I’m getting

invalid use of _. "_" represents a value to be ignored in a pattern and cannot be used in expressions

when passing pattern with _ and

this clause cannot match because a previous clause at line 23 always matches

I’m using it like so:

assert matches_pattern?({&Assembler.assemble/1, codes}, {:error, _})

How can I fix it?

I’m not sure if this is the only problem, but match?/2 takes the pattern as the first argument rather than the second.

2 Likes