Question

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.

Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top