[erlang-questions] style question - best way to test multiple non-guard conditions sequentially

Jonathan Leivent jleivent@REDACTED
Thu Jun 20 18:27:25 CEST 2013


On 06/20/2013 11:42 AM, Anthony Ramine wrote:
> Because changing it at this point would be weird. And because guards have special semantics and can't fail.
>
> Supporting expressions now would mean weird semantics like in comprehensions, where you can't distinguish a guard from an expression and know that they are compiled differently, or breaking existing code such as "if length(X) =:= 3 -> ... ; ... end": if X is not a list and that expression is evaluated with guard semantics, the first clause is skipped, whereas if it is evaluated with expression semantics, the code crashes.
>

OK - that is surprising.  That certainly changes my thinking about if 
statements.  Is this particular semantic difference between guards and 
general conditions ever expressed in the reference manual?  If so, I 
certainly missed it.

If I had read some code that just said:

   if length(X) =:= 3 -> ...

I would have inferred (incorrectly) that it would be an error for X to 
not be a list.

> If I understood things correctly, Erlang is supposed to have a cond Clauses end expression —try to evaluate "cond" in Erlang, it's a syntax error because cond is a reserved keyword—, which is the same as if but with tests compiled with expression semantics. Maybe it should be implemented for real.

Perhaps that is a remnant of "the long-ago-proposed 'cond' form" that 
Richard mentioned.

-- Jonathan




More information about the erlang-questions mailing list