Wednesday 15 August 2007

Bwuahaha :D Erlang has andalso!

And orelse! Honestly: I don't know what's the problem with short-circuit logic. Everyone loves it, as it is the most intuitive way of calculating logical values. I can see no reason for providing language syntax for both alternatives as each can be emulated in the other:

value1 = fun1()
value2 = fun2()
if value1 and value2 then [something]

Will invoke both subroutines before checking. And:

if fun1() then if fun2() then [something]

Will operate in short-circuit manner. All this and/andalso [and:>] or/orelse stuff simply pisses me off. It smells Visual-Basicish

What's more: I can see no reason for not using short-circuit operators as default in erlang as they are needed only in guards, which by default __do_not_alter_data__. The only use case I can see for non-SC operators is raising exceptions in some cases, but that's sick and ugly:

[something] when X > 0 and 5/X > Y

This will fail if you pass X=zero because "and" is not SC. I wouldn't like to work with someone, who writes such code.

Disclaimer: I just began reading "Programming Erlang", so if there IS a reason for non-SC logic as default, I'll be forced to take everything back.

1 comment:

Unknown said...

Very strange stuff. Here is what I have:

F = fun(X) when (X == 0) or (X / 0 > 2) -> true; (_) -> false end.
F(0).
false

No exception!? Why?