[erlang-questions] Dict size in guards
Fri Feb 11 08:35:36 CET 2011
On 11 feb 2011, at 03.02, Richard O'Keefe wrote:
> On 11/02/2011, at 5:05 AM, Alessandro Sivieri wrote:
>> Hi all,
>> is there a good reason for not allowing dict:size() in guards?
> Yes. Who knows what dict:size() actually does?
> Guards cannot and should not call functions.
>> I mean, it
>> looks exactly like length() for lists, in theory (of course, their
>> implementations are different).
> The inclusion of length/1 in guard tests has always been more than
> a bit dodgy. It's better taste to avoid it. Not least because
> if you do something like
> when length(L) > 10 -> long_case(L);
> when length(L) > 5 -> medium_case(L);
> when true -> short_case(L).
> doesn't do what people often think. I've several times been
> caught by that.
> f(L) ->
> N = length(L),
> if N > 10 -> long_case(L)
> ; N > 5 -> medium_case(L)
> ; true -> short_case(L)
> is safer and only evaluates the length once.
One way of avoiding length in this case is of course:
case L of
May be the compiler could take care of the "simple" cases like this, and then
issue a warning if it can not do the transformation ?
The compiler needs a fix to handle this transform in general. It uses way to many registers.
Maybe it's a problem with compiling guards?
> A style warning "length/1 in guard" would be nice to have.
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
"Have run Make so many times I dunno what's installed anymore"
More information about the erlang-questions