For example:
defmodule Math do
def add(a \ 2, b) do
a + b
end
end
What do these two bars mean?
For example:
defmodule Math do
def add(a \ 2, b) do
a + b
end
end
What do these two bars mean?
This means a default value for the last parameter, so in the example given, if no value is passed to the variable a
it will default to a match with number
2:
defmodule Math do
def add(a \ 2, b) do
a + b
end
end
If we test the function on iex
:
iex (1)> c("math.ex")
iex (2)> Math.add(1, 8) # add/2, pois estamos fazendo o match de 1 com 'a'
9
iex (3)> Math.add(8) # add/1, pois 'a' está com match padrão a 2.
10
Reference question in stackoverflow in English: link