It looks like Elixir’s docs are lacking an explanation for this behaviour.
For example, the Pattern Matching guide says:
A variable can only be assigned on the left side of
=
but does not cover the case of function parameter binding.
Additionally the Modules and Functions guide does not cover pattern matching in function parameters.
Is it accurate to say that for functions, each argument is bound to the entire expression for each parameter (déjà vu, I’m sure I’ve read this somewhere…)?
defmodule Foo do
def foo(%{a: a, c: 3} = %{b: b, c: c} = %{a: a2}) do
dbg({{a, a2}, b, c})
end
end
iex(1)> Foo.foo(%{a: 1, b: 2, c: 3})
[lib/foo.ex:3: Foo.foo/1]
{{a, a2}, b, c} #=> {{1, 1}, 2, 3}
{{1, 1}, 2, 3}
You can see here that a, a2 and b were bound from the argument, regardless of which side of which = they are.






















