your mail

David Gould davidg@REDACTED
Sun Jan 16 10:10:46 CET 2000


On Sun, Jan 16, 2000 at 10:50:04AM +1100, Bob Smart wrote:
> Why is this an illegal guard
> 
> -module(t).
> -export([test/0]).
> 
> test() ->
>   if
>     33 == list_to_integer("33") -> 99;
>     true -> 88
>   end.
> 
> but this is ok
> 
> -module(t).
> -export([test/0]).
> 
> test() ->
>   Num = list_to_integer("33"),
>   if
>     33 == Num -> 99;
>     true -> 88
>   end.
> 

Because not all BIFs are "Guard BIFs". In this case, list_to_integer/1 is
not allowed as a guard.

As best I can tell from the documentation, it looks like guards are expected
to take O(1) time. I am not sure the is a hard and fast rule, but it appears
to explain why some BIFs are ok in guards and some are not.

By the above, list_to_integer/1 which is O(n) will not be allowed in a guard.

Just speculating, but I think the intent of the restriction might be to
encourage programmers to use good Erlang style, that is use multiple
function clauses and guards, not internal case expressions. The point being
if we can count on guards being very cheap, it is easy to accept using
lots of them.

Anyone who really knows this, please feel free to give the real answer.

-dg
 

-- 
David Gould                davidg@REDACTED              510.536.1443 
- If simplicity worked, the world would be overrun with insects. -




More information about the erlang-questions mailing list