[erlang-questions] comma vs andalso

Thomas Lindgren thomasl_erlang@REDACTED
Wed Jul 8 10:38:30 CEST 2009

----- Original Message ----
> From: Richard O'Keefe <ok@REDACTED>
> Let's take two apparently similar functions:
> f(X) when is_atom(element(3, X)) ; true -> 42.
> g(X) when is_atom(element(3, X)) orelse true -> 42.
> By actual test, f(99) -> 42.
> By actual test, g(99) -> a function_clause exception.
> That is, in a guard, an expression using 'andalso' or
> 'orelse' is still an expression, and if an exception
> occurs in the left-hand operand, the whole expression
> is still skipped.
> I conclude that
> (1) you had best be EXTREMELY cautious about using 'andalso'
>     and 'orelse' in guards; they are NOT drop-in replacements
>     for ',' and ';', and
> (2) it is long past time that Erlang allowed nested use of
>     ',' and ';' in guards.

Guards are an awful mess in Erlang. I'd be quite surprised if the above difference in behaviour was intentional. If so, what possible purpose does it serve?

At one point, you could also use "and"/"or", the boolean operators manque, in guards. (I haven't checked recently whether this is still the case.) So we then have three very similar sets of guard operators.

Not to mention the twin set of type tests, introduced, as far as I know, to get rid of the lone double entendre of float/1 (as a test or conversion not the most frequent of operations). And now, for our convenience, the shorter form of these tests is being deprecated. Sigh.



More information about the erlang-questions mailing list