[erlang-bugs] List comprehension generate "function clause" exception instead of "bad generator"

Ivan Glushkov gli@REDACTED
Fri May 11 15:59:12 CEST 2012


Hi.

We have found recently an interesting behaviour:
- erlang console generate "bad generator" exception for the code:
1> [ {A,B} || {A,B} <- aaa ].
** exception error: bad generator aaa

- if compiled, the code throw "function clause" exception:

$ cat a.erl
-module(a).
-export([a/1]).
a(List) -> [ {A,B} || {A,B} <- List ].

$ erl
1> c(a).
{ok,a}
2> a:a(aaa).
** exception error: no function clause matching
                    a:'-a/1-lc$^0/1-0-'(aaa)


The reason is that new anonymous function is created to handle the
list comprehension, but it expects only lists. So incorrect generator
also would cause a "function clause" exception.

I suppose that the reason of such an exception here is for the
similarity with the lists:map, it also generates a "function clause"
for unsupported arguments:

2> lists:map(fun(A) -> A end, aaa).
** exception error: no function clause matching
                    lists:map(#Fun<erl_eval.6.80247286>,aaa)



To my mind this behavior is not correct as somebody might expect "bad
generator" exception and he will try to catch it, but instead another
exception is thrown.

Ivan



More information about the erlang-bugs mailing list