Defining infix operators in elixir?

990 views
Skip to first unread message

Ian Asaff

unread,
Dec 7, 2014, 5:56:21 PM12/7/14
to elixir-l...@googlegroups.com
I was looking through the source for the Kernal module. In looking at the code for various operators (take ">" for example), I was confused by the syntax.

The source (https://github.com/elixir-lang/elixir/blob/34df8176385200d483ab483a973c498ffa62e580/lib/elixir/lib/kernel.ex#L936) seems to imply that it's easy to implement an infix operator using the following syntax (where "op" is the operator). 

def left op right do
  # some code that does stuff given two parameters
end


so ">" is implemented like so:
def left > right do
  :erlang.>(left,right)
end

What's special about the Kernal module that allows that type of function definition? Did I miss something in the docs that specifies?

Alexei Sholik

unread,
Dec 8, 2014, 4:05:46 AM12/8/14
to elixir-l...@googlegroups.com
One special thing about Kernel is that it defines the original implementations for each operator. You can redefine any of the operators if you first unimport it inside your module.

Here's an example https://github.com/alco/pipespect/blob/master/lib/pipespect.ex#L9-L10
It can be a function or a macro depending on whether you want to delay the evaluation of the arguments or not.

Once you have a new implementation for an operator (say, in module called MyOp), you can call it as a function

MyOp.>(a, b)

or you can import it and use it as an operator

import Kernel, except: [>: 2]
import MyOp

a > b

To make it simpler, one usually defines a __using__ macro that does the unimport and import, then in the user code one just needs to write

use MyOp

a > b

See the link above for an example of this.

There are some operators that don't have implementations by default. Those can be implemented without unimport from Kernel – https://github.com/elixir-lang/elixir/blob/0b1c3b7d284da4b0b99b4f0cc172d6e37f6c43ea/lib/elixir/lib/macro.ex#L20

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ian M. Asaff

unread,
Dec 8, 2014, 9:21:30 AM12/8/14
to elixir-l...@googlegroups.com
Alexei,

Thanks so much for the detailed explanation. It's much appreciated!

Ian

--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-talk/pTde-7d52QY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-ta...@googlegroups.com.

Roger Qiu

unread,
Dec 10, 2014, 11:32:42 PM12/10/14
to elixir-l...@googlegroups.com
Does Elixir support custom operators yet?

José Valim

unread,
Dec 10, 2014, 11:36:11 PM12/10/14
to elixir-l...@googlegroups.com
No, and it isn't scheduled as a feature for now. There are some operators available though (they are not used anywhere in the stdlib) and Alexei linked to them.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--


José Valim
Skype: jv.ptec
Founder and Lead Developer

Reply all
Reply to author
Forward
0 new messages