What do the characters \\ mean in the parameter of a function in Elixir?

1

For example:

defmodule Math do   
   def add(a \ 2, b) do
      a + b   
   end 
end

What do these two bars mean?

    
asked by anonymous 18.02.2018 / 20:28

1 answer

1

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

    
18.02.2018 / 20:28