Writing a macro that defines infix operators

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?

There is only a coupld of valid infix operators that you can use.

Plase see Operators reference — Elixir v1.17.3 for more information.

2 Likes

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?)

infix_function(:"+", adder/2) could be made work roughly by this, IIRC how things work:

defmodule M do
  defmacro infic_function(op, fun)
    quote do
      def unquote(op)(a, b), do: unqote(fun)(a, b)
    end
  end
end

You can actually use the same trick for “simpler” pattern matching…

def unquote(:"*")(%MyStruct{} = a, b), do: # impl

Whether or not this is actually “simpler” us left to you…

2 Likes