6 Checks, Errors, and Tests

6.1 Guards

6.1.1 Guards in Erlang

6.1.2 Guards in LFE

6.1.3 Examples

6.1.4 Limitations

The Erlang VM only allows a limited set of expressions in guards:

  • comparison operators (==, !=, ===, !==, >, <, <=, >=)
  • boolean operators (and, or) and negation operators (not, !)
  • arithmetic operators (+, -, *, /)
  • <> and ++ as long as the left side is a literal
  • (orelse) and (andalso)
  • all the following type check functions:
    is_atom/1
    is_binary/1
    is_bitstring/1
    is_boolean/1
    is_float/1
    is_function/1
    is_function/2
    is_integer/1
    is_list/1
    is_map/1
    is_number/1
    is_pid/1
    is_port/1
    is_reference/1
    is_tuple/1
  • plus these functions:
    (abs number)
    (bit_size bitstring)
    (byte_size bitstring)
    (div integer integer)
    (elem tuple n)
    (hd list)
    (length list)
    (map_size map)
    (node)
    (node pid)
    (node ref)
    (node port)
    (rem integer integer)
    (round number)
    (self)
    (size tuple)
    (size bitstring)
    vtl list)
    (trunc number)
    (tuple_size tuple)