[erlang-questions] This clause cannot match because....
Stefan Hellkvist
hellkvist@REDACTED
Mon May 8 14:54:08 CEST 2017
Hi all,
I was just struck by a bug in my code due to wrong ordering of clauses.
Given a function f/1 which could be simplified to something like this:
f([a, B]) ->
B;
f([a, b]) ->
will_never_happen.
The problem is that the clause f([a, b]) is never taken simply because it
is fully covered by the first clause which matches all values for the
second element in the list. If I had put the second clause before the more
general clause it would have worked the way I thought it should.
I'm curious though. Why did the compiler not spot the problem in the code
for f/1 when it can obviously spot the problem in a, to me at least,
similar function f/2 seen below?:
f(a, B) ->
B;
f(a, b) ->
will_never_happen.
In this example, (f/2), I get a compilation error with a very good and
detailed description of what the problem is, but in the first case (f/1)
the compiler does not see the problem (or cannot?) with the covering
clause.
Is this some edge case where the compiler has just stopped caring and I
must use dialyser to spot the problem at an earlier stage than at runtime?
Stefan
p.s. funny thing is that if I use a tuple instead of a list to create f/1
in the same way the compiler is once again able to spot the problem, but
not with a list as argument.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170508/e206b64c/attachment.htm>
More information about the erlang-questions
mailing list