[erlang-questions] Lack of warning for overlapping clauses with

Bernard Duggan bernie@REDACTED
Mon Jul 19 08:34:46 CEST 2010


You're right, but the catch is that your second example doesn't produce 
a warning either.  There may, of course, be a very good reason that 
these cases don't do so, but the difference between == and =:= is not it.

B

On 19/07/10 16:17, Hynek Vychodil wrote:
> 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
>>
>>      
>
>
>    



More information about the erlang-questions mailing list