Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir default parameters for named functions with multiple clauses

I have trouble understanding how default parameters interact with multiple clauses in named functions. It boils down to, why does the following snippet work?

defmodule Lists do

  def sum([], total \\ 0), do: total
  def sum([h|t], total), do: h + sum(t, total)

end

From my understanding this gets expanded by the compiler into:

defmodule Lists do

  def sum([]), do: sum([], 0)
  def sum([], total), do: total
  def sum([h|t], total), do: h + sum(t, total)

end

So I would expect the following to happen:

iex(1)> Lists.sum [1,2,3,4]
** (FunctionClauseError) no function clause matching in Lists.sum/1

instead it works:

iex(1)> Lists.sum [1,2,3,4]
10

Using Elixir 0.12.4.

like image 472
Alexander Battisti Avatar asked Feb 27 '14 21:02

Alexander Battisti


1 Answers

Actually, def sum([], total \\ 0), do: total will define a function clause that looks like def sum(list), do: sum(list, 0). So I can definitely see your confusion. I will guarantee we emit a warning for such cases in future releases. Thank you!

like image 118
José Valim Avatar answered Nov 09 '22 16:11

José Valim