I am trying to write a macro to define a custom operator
defmacro infix_function(op, function) do
quote do
def (a unquote(op) b), do: unquote(function)(a, b)
end
end
But I am getting a syntax error on the def (a unquote(op) b) bit. I am not sure if there is an alternative syntax to define operators that would let this work?
As an aside, when pattern matching while writing an operator I am having to do something like this:
def ((%MyStruct{} = a) * b), do: MyStruct.mult(a, b)
Is there an alternative syntax to defining infix operators that is better suited to pattern matching?
I have read that page. So is the issue that the unquote(op) could be result in an invalid operator?
With my code above the actual macro definition does not compiles, I was trying to do e.g. infix_function(+, blah) (which also has a compile error, I assume because + is not valid there?)