The Untold Elixir Secret on Operator Re-Definitions : + to -

Malreddy Ankanna
blackode
Published in
4 min readDec 10, 2017

--

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.

--

--

Malreddy Ankanna
blackode

Programmer & Writer, I write about coding, thoughts, ideas, personal musings, technical articles, and tutorials.https://bio.link/blackode