Alex Rivera | Logout

Can I make my own Guards in Erlang?

Asked 2012-06-24T11:36:08.157
10

I came accross this code on the web:

is_char(Ch) ->         
    if Ch < 0 -> false;  
       Ch > 255 -> false;
       true -> true      
    end.

is_string(Str) ->            
    case is_list(Str) of           
    false -> false;           
    true -> lists:all(is_char, Str)
    end.

Its is the Guard I alwais dreamed off, in that it checks if an input is a string -- how ever, I'm not allowed to use it in erlang, why is this? And is there a work around?

I would like to be able to write stuff like:

Fun(Str) when is_string(Str) -> Str;
Fun(Int) when is_integer(Int) -> io:format("~w", [Int]).

or even better use it on messages.

Edit
Report

1 Answer

13

You are not allowed to use user defined functions in the guards. It is because the functions in the guards have to be free from side effects (such as using io:format in your functions). In guards, you are limited to the following:

  • BIFs used for type tests (is_atom, is_constant, is_float, is_integer, is_list, is_number, is_pid, is_port, is_reference, is_tuple, is_binary, is_function, is_record),
  • boolean operators (not, and, or, andalso, orelse, ,, ;),
  • relational operators (>, >=, <, =<, =:=, ==, =/=, /=),
  • arithmetic operators (+, -, *, div, rem),
  • bitwise operators (band, bor, bxor, bnot, bsl, bsr),
  • other BIFs that are free of side effects (abs/1, element/2, hd/1, length/1, node/1,2, round/1, size/1, tl/1, trunc/1, self/0)
answered 2012-06-24T11:54:13.737

Your Answer