[erlang-questions] Guards?
Roberto Ostinelli
roberto@REDACTED
Tue Apr 5 15:06:39 CEST 2016
> What you're missing is operator precedence [1] - "and" takes precedence
> over "<", so your guard clause ends up looking, to the runtime, like what
> Bengt pointed out - you have the boolean expression `is_inteteger(X) or
> is_float(X)` followed by and, followed by X - which is evaluated before the
> < operator. The evaluation of your guard looks something like this:
>
> X = 1
> (((is_integer(1) or is_float(1)) and 1) < 5
>
> (((true) and 1) < 5)
>
> `true and 1` cannot be evaluated (throws an exception, as `and` doesn't
> take integer arguments). Now, your guard expression threw an exception -
> what happens then:
>
> "If an arithmetic expression, a Boolean expression, a short-circuit
> expression, or a call to a guard BIF fails (because of invalid arguments),
> the entire guard fails. If the guard was part of a guard sequence, the next
> guard in the sequence (that is, the guard following the next semicolon) is
> evaluated." [2]
>
> So, your guard ends up not matching - you have no other function clauses,
> so you have "no function clause matching."
>
> Make sense now?
>
> Doug
>
Thank you Doug. Yes - it makes sense. However operator precedence, which I
previously checked, do not specify and & or:
http://erlang.org/doc/reference_manual/expressions.html#prec
Therefore I assumed precedence was the same, and that is confusing.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160405/96881a49/attachment.htm>
More information about the erlang-questions
mailing list