[erlang-questions] Exceptions in guards are blocked. Why?
Sat Jan 31 18:44:44 CET 2009
2009/1/29 Richard O'Keefe <ok@REDACTED>
> > Is this cause of 'side effects free' guards nature?
> Not really. It's because guards aren't true or false;
> they succeed or fail. (Blurring this distinction was a
> really bad idea.)
This point Richard is making is actually very basic and important, guards
aren't true or false but rather they either succeed or fail. Just to restate
it! Guards are really just the bits of pattern matching which can't easily
be written in the pattern. So really the only way a clause is chosen is by
pattern matching alone.
In this context it was quite reasonable to add the feature that an exception
in a guard was equivalent to the guard failing. It also explains why some
guard *tests* aren't really valid as expressions, for example the *test*
float(F) tests whether F is a float, while the *expression* float(F) means
convert F to a float (if it is an integer).
Unfortunately adding boolean operators, while being useful, makes guards
look like expressions and blurs the distinction between expressions and
guard tests. It also complicates the semantics of exceptions in guards.
P.S. Yes I know there exists ways of writing patterns were the tests are
included in the pattern, I have seen some, but I think they the pattern very
hard to read. Having them in a separate guard does make it easier to read
(but maybe harder to understand).
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions