Lack of warning for overlapping clauses with

Torben Hoffmann <>
Thu Jul 15 13:31:40 CEST 2010


Hi,

I had a very large module where I could not figure out why a specific thing
was not happening and it turned out that the root of the problem was a
construct like the following:

-module(problem).

-compile(export_all).

f(N) when N==1; N==2 ->
    false;
f(1) ->
    ok.

I would have hoped that the compiler or dialyzer would have caught that the
f(1) clause will never be executed, but that is not the case.
It seems that the use of the when-clause changes the game so that the
overlap is not detected.
I accept that I am shooting myself in the foot here, but I would have liked
to be helped by the compiler or dialyzer since figuring this one out after
going through hefty logs is not fun.

Now that I know this I can work around it, but it would be nice to get the
warning since the code in my f(N) branch is actually not that small. Of
course I could create a function to do the operation and rewrite like this:

-module(problem).

-compile(export_all).

f(1) ->
    g();
f(2) ->
    g();
f(1) ->
    ok.

In which case the compiler will warn me about the overlap, but I find that
to be somewhat ugly.

Any comments on this?

Cheers,
Torben
-- 
http://www.linkedin.com/in/torbenhoffmann


More information about the erlang-questions mailing list