Qqwy

Qqwy

TypeCheck Core Team

What infix operators would you like to see in Elixir?

In Elixir, operators are also simply functions. A unary operator takes a single argument, a binary infix operator takes two.

It is therefore possible to define our own. However, the compiler determines which strings of characters are allowed as operators, so there is only a small set of possibilities.

Most of these are already defined in one way or another in Elixir’s standard library. In the end, the only operators that can be used without overriding an operator either in Kernel, Kernel.SpecialForms or Bitwise are:

\\ (which is also used to specify a ‘default’ argument in function clauses),
<- (which is also used inside for-comprehensions)
| (which is also used to construct/pattern match lists)
and
~>, <~, ~>>, <<~, <~>, <|>.

So: When you want to unequivocally define an operator in Elixir, you’re restricted to this set of squiggely arrows.

I personally think that this list is rather small. I totally agree that Haskell’s approach of letting you define any combination of non-alphanumeric symbols means that it is impossible for an outsider to comprehend the code, as operators are very implicit (you cannot find out what they do just by reading them).

On the other hand, operators ensure that code is very concise, and some types of operations are easier to read/write when written in an infix style, rather than a prefix one.

Elixir’s currently only allows a very small number of user-definable operators, I find this list to be rather short. Multiple times now, I’ve wanted to implement an operator for my library, and I was unable to find a good one.

On a related note, when two libraries both want to add an operator, and they end up using the same one, you cannot use those libraries in your module at the same time (unless you call one of the operators remotely).


So: What operators would you like to see being added (as free, user-definable operators) to Elixir?

Most Liked

Onor.io

Onor.io

Takes judgment to know where to best use macros. Good judgment comes from experience and experience comes from bad judgment. The problem is that our bad judgment lives on forever in code others have to maintain.

rvirding

rvirding

Creator of Erlang

Why not just use APL? Sorry I believe something like this would very quickly result in potentially large amounts of unreadable code. Perhaps not for the writer but definitely for future maintainers. It would be the major issue with macros on steroids.

Korbin73

Korbin73

Wouldn’t a macro solve the same problem? The trade off in reasoning (or lack of) about sparsely used macros would be the same as a custom operator. At the very least, named infix operators might not be a bad idea, but doing custom infix operators would hurt the approachability of the language I think.

Last Post!

rvirding

rvirding

Creator of Erlang

Yes, of course you can do that. It actually has the benefit that you separate your DSL from Elixir so it is much clearer what is what. Otherwise you are basically writing something which looks like Elixir but isn’t because of the operators.

And yes I will admit I am rather strict on that if you want a DSL then make a real one and don’t try sneak it in to the surrounding language, in this case Elixir.

Where Next?

Popular in Discussions Top

rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
acrolink
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement