In Elixir, function uniqueness is defined with:
- Module name
- Function name
- Function arity
That takes us to function name overloading. I heard the first time verb overloading in C++.
Overload [verb] load with too great a burden or cargo.
In Elixir, we can have two functions in the same module with the same name, but with different arity:
In the example above, we have two
Salary.Contract.developer functions, with arity 0
Salary.Contract.developer/0 and one with arity one,
Start IEx and run
Remember how arity is documented with
/ because you will read a lot of Elixir documentation.
We use Elixir function arity overloading in practice so we have a function with the largest arity with all the code, and functions with lower arity would call that “largest” function.
In Elixir code, you will also find notation for parameter default values.
Remember, when you use default values using
\\ you will create at least two functions with the same function definition. The reason is that we get more functionality with less coding.
When you omit rate parameter in a function call, the rate gets value after
- what is arity
- how arity is documented
- how to use function overlading in practice
- default values
Originally published at https://blog.tentamen.eu on June 5, 2020.