[erlang-questions] erlang sucks

Sean Hinde sean.hinde@REDACTED
Tue Mar 11 21:06:03 CET 2008


On 11 Mar 2008, at 19:26, Mats Cronqvist wrote:

> Sean Hinde wrote:
>>
>> On 11 Mar 2008, at 12:47, Mats Cronqvist wrote:
>>
>>>
>>> * 'if' is  completely worthless, and should ideally be obsoleted.
>>
>> No, no no! I like 'if' It allows some neat constructs that are  
>> horrible with case.
>
> i was afraid no one was going to bite... still, I'd like an example  
> or 2.

I'm thinking of things like:

case X of
	_ when X == abc; X == xyz ->
		do_abc_or_xyz();
         _ when X == '123', X == '456' ->
		do_123456()
	'123' ->
		do_123_only();
         fred ->
                 do_fred()
end

vs

if
	X == abc;
	X == xyz ->
		do_abc_or_xyz();
         X == '123',
	X == '456' ->
		do_123456();
	X == '123' ->
		do_123_only();
         X == fred ->
                 do_fred()
end

I don't much like the _ when business.

'if' is also useful when you want to branch on a bunch of unrelated  
boolean variables in various combinations:

case {X,Y,Z} of
	{true, false, _} ->
		do_a();
         {false, _, true} ->
		do_b();
	_ ->
		do_c()
end

vs

if
	X and not Y ->
             do_a;
	not X and Z ->
             do_b;
	true ->
             do_c
     end.

I find the second more clearly states the intent. You also don't have  
to remember the positions in the tuple to parse the code (though you  
do need to know that 'not' is stickier* than 'and')

Sean

* IANACS




More information about the erlang-questions mailing list