A guard sequence is a sequence of guards, separated by semicolon (;). The guard sequence is true if at least one of the guards is true. (The remaining guards, if any, are not evaluated.)
A guard is a sequence of guard expressions, separated by comma (,). The guard is true if all guard expressions evaluate to true.
The set of valid guard expressions (sometimes called guard tests) is a subset of the set of valid Erlang expressions. The reason for restricting the set of valid expressions is that evaluation of a guard expression must be guaranteed to be free of side effects. Valid guard expressions are the following:
- The atom true
- Other constants (terms and bound variables), all regarded as false
- Calls to the BIFs specified in table Type Test BIFs
- Term comparisons
- Arithmetic expressions
- Boolean expressions
- Short-circuit expressions (andalso/orelse)
Table 8.4: Type Test BIFs
Notice that most type test BIFs have older equivalents, without the is_ prefix. These old BIFs are retained for backwards compatibility only and are not to be used in new code. They are also only allowed at top level. For example, they are not allowed in Boolean expressions in guards.
Table 8.5: Other BIFs Allowed in Guard Expressions
If an arithmetic expression, a Boolean expression, a short-circuit expression, or a call to a guard BIF fails (because of invalid arguments), the entire guard fails. If the guard was part of a guard sequence, the next guard in the sequence (that is, the guard following the next semicolon) is evaluated.