The Secret behind Elixir Operator Re-Definitions : + to -

Blackode
· 4 min read

Just for Fun

Elixir 1.5.2

Hello, everyone. Today, we are going to do some crazy stuff. This article is just for fun. The only intention is to show you this can possible in elixir.

The universal meaning of + is to add but for a change, we are making this as subtraction.

How to achieve this ?

We can use def* constructs (def, defp, defmacro, …) for re-defining the operators in elixir.

The Only 1 Rule

The only rule is the name of definition. It should be same as how we use the operator while coding. In general, we use + as a + b So, our definition name should be same as a + b .

Let’s do things in Wrong

All your operator re-definitions should reside inside the module because the definitions cannot stand outside the module.

Check the following code for re-defining + to -

defmodule MyWrongOperators do  def a + b do
a - b
end
end

Nothing is new here. We have just created a function or definition but with a little tricky name.

Boom!! You have turned the world upside down. We are doing things as they are possible and elixir allowed us to do. But these are nothing to do.

How to use?

Here, in elixir, there exists a power that pulls you back from using even though it is allowed you to create with out any hurdles.

If you force to use the module like in regular style of coding, it hits you with an exception. Lets check that.

iex> import MyWrongOperatorsiex> 1 + 2** ** (CompileError) iex:4: function +/2 imported from both MyWrongOperators and Kernel, call is ambiguous
Compile Error When Using the Module Directly

Well, what is the power throwing you this error by not allowing? Can you think for a while and take a guess?


I don’t want to linger you anymore. The Kernel module loaded by default. This Kernel module contains a +/2 function which is also loaded. So, when we are importing our module MyWrongOperators , we loaded +/2 function again. Now with in our scope, there exist two +/2 definitions. So, the compiler got confused here.

How to bypass this ?

Well, we need all the functions in Kernel module except the +/2. This can be achieved with except while loading a module.

import Kernel, except: [+: 2]
import MyWrongOperators

The above LOC clearly tells not to load the +/2 function. So, we only have one +/2 definition from the module MyWrongOperators . So, the compiler can now use +/2 definition with out getting confused.

iex > import Kernel, except: [+: 2]
Kernel
iex> import MyWrongOperators
MyWrongOperators
iex> 1 + 2
-1 # 1 - 2 operation performed here as we defined.
The perfect Usage

In the above screen shot, at iex(3) line we are loading the Kernel module with out letting it to load +/2 definition. So, it worked perfectly.

We successfully executed the wrong operations.

Things to Remember

Q. Can I create new operator ?
Ans. No. But, Elixir is capable of parsing a predefined set of operators like

  • |
  • |||
  • &&&
  • <<<
  • >>>
  • ~>>
  • <<~
  • ~>
  • <~
  • <~>
  • <|>
  • ^^^
  • ~~~

Q. Can I Override every other operators ?
Ans. Yes. You can. But, while using don’t forget to import Kernel module with those override operators as exceptional.

Q. Is it good to override operators ?
Ans. Of course it not good and not recommended.

Q. Why it is not recommended ?
Ans. Custom-defined operators can be really hard to read and even more to understand, as they don’t have a descriptive name like functions do.

Live Demo

https://asciinema.org/a/151933
Clap if it is worth.

Warning ~~
Highly not recommended to do…

Happy Coding !!

blackode

Coding, thoughts, and ideas.

Blackode

Written by

Blackode

Wrong CODE takes U to the right BUG 🐞 coding, thoughts & ideas

blackode

blackode

Coding, thoughts, and ideas.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade