Elixir default parameters for named functions with multiple clauses
Asked Answered
L

1

5

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.

Latinize answered 27/2, 2014 at 21:22 Comment(0)
J
13

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!

Juna answered 27/2, 2014 at 21:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.