[erlang-questions] comma vs andalso

Richard Carlsson <>
Wed Jul 8 14:30:58 CEST 2009


Thomas Lindgren wrote:
> Not to mention the twin set of type tests, introduced, as far as I
> know, to get rid of the lone double entendre of float/1 (as a test or
> conversion not the most frequent of operations). And now, for our
> convenience, the shorter form of these tests is being deprecated.
> Sigh.

Oh, and I forgot this beauty, which is a consequence of the
doubleplusweird scope rules for the old type tests in guards:

  1> if integer(42) -> ok; true -> no end.
  ok
  2> if integer(42) == true -> ok; true -> no end.
  * 1: illegal guard expression

because, you see, it is only at the absolute top of the guard test
that the name 'integer(X)' can be used to refer to the type test.
Shove it into one side of an '==', and it is no longer recognized.
The new forms work everywhere:

  3> if is_integer(42) == true -> ok; true -> no end.
  ok
  4> if is_integer(42) == true -> ok; true -> no end.
  ok

And for my final trick, here's the old double entendre you mentioned:

  5> if float(3.14) -> ok; true -> no end.
  ok
  6> if float(3.14) == true -> ok; true -> no end.
  no

why, isn't it obvious? The first clause uses the type test float(X),
while the second ensures that 3.14 is cast to a float and then
compares the number to 'true'.

Sticking to the modern is_-forms of the type tests lets you stay sane.

    /Richard


More information about the erlang-questions mailing list