The Secret behind Elixir Operator Re-Definitions: + to -
blackode
Posted on August 5, 2018
Photo by rawpixel.com on Unsplash
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 a 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 as we all know the fact that 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 just doing the things as they are possible and elixir allowed us to do. But these are nothing to do in the real world. Just for fun.
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 MyWrongOperators
iex> 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.
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
Warning
Highly not recommended to do…
Happy Long Coding Life :)
Posted on August 5, 2018
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.