
An Erlang WAT-story about guards and guards - elbrujohalcon
https://medium.com/@elbrujohalcon/there-are-guards-and-guards-71e67d4975d7#.7yc664pkc
======
yetihehe
In erlang 'and' has higher priority than comparisons. So 'is_integer(X) and X
> 0' is interpreted as '(is_integer(X) and X) > 0'. 'andalso' has lower
priority than comparisons so it is interpreted as we expect.

------
njharman
I really don't understand the aversion?, lack of use? of parenthesis. Back in
college (before C++ was released to give you time scale) some C class, spent a
lot of time on operator precedence. I was like, why bother? Just use parens,
be explicit, clear and never ever have to learn or be bit by any language's
precedence ever!

Ever.

------
pepesza
Just FYI. Brujo is close to being expert in Erlang. This post is a more of a
"warning for juniors" than "damn, today I've discovered something new".

And there is nothing really WAT about it. Just part of rather simple Erlang
language grammar.

~~~
Tehnix
I would argue that the precedence priority is kinda a wat-moment. Most other
languages[0][1] have the logical operators `and`, `or` and `not` in the lowest
(or near) priority, which makes much more sense IMHO.

If you'd dismiss the above, then I guess one could also argue that JavaScript
returning an empty string on `[] + []` is also not a wat moment, but just part
of the JavaScript grammar.

[0]
[http://kevincantu.org/code/operators.html](http://kevincantu.org/code/operators.html)

[1]
[http://www.tutorialspoint.com/python/operators_precedence_ex...](http://www.tutorialspoint.com/python/operators_precedence_example.htm)

~~~
mst
It is definitely an oddity that it's and/andalso instead of &&/and.

Similar to the fact that shell and perl swap == versus eq; that causes me no
end of confusion switching between the two.

Though nothing will ever be as upsetting as the period when I used QBASIC at
school and BBC BASIC at home. Number of times my attempts to use COLOUR
compiled first time? Not even once.

------
Luc
I always use ',' and ';' in guards. Maybe I'm weird but it feels wrong using
the short-circuit operators knowing that guards are always short-circuited
anyway. Also they take up too much space :)

~~~
qbrass
Erlang makes much more sense if you've had experience with Prolog.

------
chriswarbo
Reminded me of `and`, `or`, etc. precedence in PHP, e.g.
[http://php.net/manual/en/language.operators.precedence.php#1...](http://php.net/manual/en/language.operators.precedence.php#117390)

