[erlang-questions] Strange behaviour of element/2 inside list comprehensions

Bjorn Gustavsson bjorn@REDACTED
Tue Sep 25 11:15:59 CEST 2007


Oscar Hellström <oscar@REDACTED> writes:

> Hi,
> 
> I've noticed that element/2 doesn't exit with a badarg as I expected 
> when used in a list comprehension. Other functions that exit with badarg 
> are behaving as expected though. Is this actually the intended 
> behaviour? (I'm using R11B-4 but have also seen it on R11B-3).
> 
> Example:
> 1> [Foo || Foo <- [{1,2}], element(3, Foo) /= 9].
> []
> 5> [Foo || Foo <- [{1,2}], list_to_integer(Foo) /= 10].

Guard BIFs (such as element/2) will be executed in guard context and will not cause an
exception. All other BIFs and functions call will cause an exception.

The reason for this difference is because the original implementation did it this way
(probably as an optimization or because it was simplest). When we later wanted to change
that in the new Beam compiler for R6B, we discovered that a lot of code in OTP depended
on this behaviour, so we kept it.

/Bjorn
-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list