Can I make my own Guards in Erlang?

前端 未结 2 456
日久生厌
日久生厌 2020-12-20 14:37

I came accross this code on the web:

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


        
相关标签:
2条回答
  • 2020-12-20 15:32

    Another reason for not allowing user defined functions in guards is that errors are handled differently in guards than in "normal" functions. In a guard an error does not generate an exception, it only causes the guard itself to fail.

    Guards are not really expressions but tests.

    0 讨论(0)
  • 2020-12-20 15:37

    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)
    0 讨论(0)
提交回复
热议问题