[erlang-questions] Lack of warning for overlapping clauses with
Hynek Vychodil
hynek@REDACTED
Mon Jul 19 08:17:56 CEST 2010
f(N) when N==1; N==2 ->
false;
f(1) ->
ok.
is not same as
f(1) ->
false;
f(2) ->
false;
f(1) ->
ok.
Following is:
f(N) when N=:=1; N=:=2 ->
false;
f(1) ->
ok.
Former is hard to realize for compiler even it is possible.
On Thu, Jul 15, 2010 at 1:31 PM, Torben Hoffmann
<torben.lehoff@REDACTED> wrote:
> 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
>
--
--Hynek (Pichi) Vychodil
Analyze your data in minutes. Share your insights instantly. Thrill
your boss. Be a data hero!
Try GoodData now for free: www.gooddata.com
More information about the erlang-questions
mailing list