Thanks!
same({1,2},3)
could be interpreted verbatim as calling same/2
or as
same({1,2}, 3, 1)
due to the optional parameter on same/3
.
How is the compiler supposed to know which one you mean?
Then I din’t understand the explanaiton of @NobbZ because what I understood is not that same({a, b}, c)
could be interpreted as a call to same/2
or same/3
and then it check what call to do. I though what it is expanded to 2 definitions, then the previous call always will be same/2
and then as the pattern matching in the argument is before of same arity without that it’ll match with the first (as it’s expected).
@NobbZ said:
def foo(bar, baz \\ []), do: ...
def foo(quux, bar, baz \\ %{}), do ...
is getting expanded at compile time to the following:
def foo(bar), do: foo(bar, [])
def foo(bar, baz), do: ...
def foo(quux, bar), do: foo(quux, bar, %{})
def foo(quux, bar, baz), do: ...
Then, in that example 2 functions have same arity and don’t do pattern matching in one, then I won’t can call to both; but with the pattern matching in one argument, if this is before of argument without it, I’ll can call to both. Of course, there is not so much sense to order with same arity without the tuple matching in one argument first because then nothing will match because it’s checked in order. But, if:
def foo({bar, bax}, baz \\ []), do: ...
def foo(quux, bar, baz \\ %{}), do ...
is getting expanded at compile time to the following:
def foo({bar, bax}), do: foo({bar, bax}, [])
def foo({bar, bax}, baz), do: ...
def foo(quux, bar), do: foo(quux, bar, %{})
def foo(quux, bar, baz), do: ...
As, if it’s correct, the expansion is in the function code, not in the call, a call as foo({1, 2}, 3)
is always foo/2
then it must match with:
def foo({bar, bax}, baz), do: ...
def foo(quux, bar), do: foo(quux, bar, %{})
And because the order is correct, with the argument that match the tuple before, then it’ll match with first of foo/2
. Then my second example in first post:
...(8)> def same({a, b}, c \\ 1), do: 1
...(8)> def same(a, b, c \\ 1), do: 3
if expands to:
def same({a, b}), do: foo({a, b}, 1)
def same({a, b}, c), do: ...
def same(a, b), do: foo(a, b, 1)
def same(a, b, c), do: ...
Must works (and it works if I write as it’s expanded), then if compiler can’t expand a code that’s correct when it’s write as expanded, isn’t it an error? I understand that if it only check arity then try to avoid error, but when it as matchings in arguments it could be correct and works. Only problem could be if it doesn’t expand the function definition and it expand functions calls, that I didn’t understood that way.
The behaviour you are looking for seems to be this:
iex(1)> defmodule Test do
...(1)> # same/1
...(1)> def same({_,_} = arg), do: same(arg, 1)
...(1)> # same/2
...(1)> def same({_a,_b}, _c), do: 2
...(1)> def same(a,b), do: same(a,b, 1)
...(1)> # same/3
...(1)> def same(_a, _b, _c), do: 3
...(1)> end
Yes, I used 1, 2 &3 only to show in console where it did the call, but that’s the correct behaviour. What I tried to tell is what that could be written as:
iex(1)> defmodule Test do
...(1)> def same({_a,_b}, _c \\ 1), do: 2
...(1)> def same(_a, _b, _c \\ 1), do: 3
...(1)> end
And expanded to:
iex(1)> defmodule Test do
...(1)> # same/1
...(1)> def same({_,_} = arg), do: same(arg, 1)
...(1)> # same/2
...(1)> def same({_a,_b}, _c), do: 2
...(1)> def same(a,b), do: same(a,b, 1)
...(1)> # same/3
...(1)> def same(_a, _b, _c), do: 3
...(1)> end
But:
iex(22)> defmodule Test do
...(22)> def same({_a,_b}, _c \\ 1), do: 2
...(22)> def same(_a, _b, _c \\ 1), do: 3
...(22)> end
** (CompileError) iex:24: def same/3 defaults conflicts with same/2
iex:24: (module)
When the expected expand will work well and won’t raise errors compiling the expanded version.
Then, maybe compiler could not to raise the error when even arity is same the arguments have something that can match, specially when the order is correct (if order is not good compiler show a warning), not to force to write the expanded version when the idea of \\
, I think, is not to write the expanded version, to do same with less, compiler is not avoiding error, is raising an error in a code that expanded will work well.
EDIT: Or I’m ignoring something I don’t know/understood about expansion and the compiler and it makes sense not to do that expansion and avoid it to avoid errors.