Hacker News new | past | comments | ask | show | jobs | submit login
An Erlang WAT-story about guards and guards (medium.com/elbrujohalcon)
52 points by elbrujohalcon on July 7, 2016 | hide | past | favorite | 10 comments



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.


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.


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.


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

[1] http://www.tutorialspoint.com/python/operators_precedence_ex...


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.


C/C++ also have & and | in the wrong place, mostly thanks to their heritage as being overloaded for && and ||).

I've personally seen several (flags & MASK != VALUE) bugs make it to the field. Sometimes it's worked for ages as the 0th bit was part of what was being tested.

Whilst adding more brackets can be useful to explain things, there comes a point when you have too much parenthesis. You have to rely at least a little on operator precedence, so you really do need to learn it.


HAHA! Thanks @pepesza :)


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 :)


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


Reminded me of `and`, `or`, etc. precedence in PHP, e.g. http://php.net/manual/en/language.operators.precedence.php#1...




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: