[erlang-questions] learning Erlang: factoring primes

Kostis Sagonas kostis@REDACTED
Fri May 4 13:58:30 CEST 2007

Matthias Lang asked me:
> Is there something here I've missed, or are you just being AR?
> AFAIK, == doesn't do any coercing if both  arguments are integers,
> i.e. there is no danger at all in using == in the example below.

Admittedly, because your example uses 'rem' it makes no difference.
A sufficiently clever compiler could know out that 'rem'/2 returns
an integer and change this to '=:='/2 automatically.  However, BEAM
does not do anything like that or takes advantage of type information.

More generally, in an expression of the form X == 0, the compiler is
only allowed to infer (and take advantage of this information by 
avoiding tests on tags) that X will be a number in the success branch.

In an expression of the form X =:= 0, the compiler can easily infer that
in the success branch X will be an integer.  This is much better
information, not only for optimization, but also for other tools (e.g.
for Dialyzer).

I've seen tons of places in Erlang code where people mean =:=/2 and
they use ==/2.  In my opinion this bad practice should change.  Also,
it is really a pity that Erlang does not have proper integer comparison
operators and all of them (e.g. '>'/2) are overloaded for numbers.


More information about the erlang-questions mailing list