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

Robert Virding rvirding@REDACTED
Tue Sep 25 11:58:17 CEST 2007


On 25 Sep 2007 11:15:59 +0200, Bjorn Gustavsson <bjorn@REDACTED>
wrote:
>
> 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.


It was an optimisation, and not because it was simpler to do. It is in fact
slightly more difficult, but not much.

Robert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20070925/0146ba6d/attachment.htm>


More information about the erlang-questions mailing list