gilbertbw

gilbertbw

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?

Most Liked

NobbZ

NobbZ

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

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

NobbZ

NobbZ

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…

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement