@@ -54,13 +54,15 @@ The first clause above will only match when `x` is positive.
54
54
55
55
## Expressions in guard clauses
56
56
57
- The Erlang Virtual Machine (VM) only allows a limited set of expressions in guards:
58
-
59
- * comparison operators (` == ` , ` != ` , ` === ` , ` !== ` , ` > ` , ` < ` , ` <= ` , ` >= ` )
60
- * boolean operators (` and ` , ` or ` ) and negation operators (` not ` , ` ! ` )
61
- * arithmetic operators (` + ` , ` - ` , ` * ` , ` / ` )
62
- * ` <> ` and ` ++ ` as long as the left side is a literal
63
- * the ` in ` operator
57
+ Elixir imports and allows the following expressions in guards by default:
58
+
59
+ * comparison operators (` == ` , ` != ` , ` === ` , ` !== ` , ` > ` , ` >= ` , ` < ` , ` <= ` )
60
+ * boolean operators (` and ` , ` or ` , ` not ` )
61
+ * arithmetic operations (` + ` , ` - ` , ` * ` , ` / ` )
62
+ * arithmetic unary operators (` + ` , ` - ` )
63
+ * the binary concatenation operator ` <> `
64
+ * the ` in ` operator as long as the right side is range or a list
65
+ * all operators in the` Bitwise ` module (` bnot ` , ` ~~~ ` , ` band ` , ` &&& ` , ` bor ` , ` ||| ` , ` bxor ` , ` ^^^ ` , ` bsl ` , ` <<< ` , ` bsr ` , ` >>> ` )
64
66
* all the following type check functions:
65
67
* ` is_atom/1 `
66
68
* ` is_binary/1 `
@@ -80,6 +82,7 @@ The Erlang Virtual Machine (VM) only allows a limited set of expressions in guar
80
82
* ` is_tuple/1 `
81
83
* plus these functions:
82
84
* ` abs(number) `
85
+ * ` binary_part(binary, start, length) `
83
86
* ` bit_size(bitstring) `
84
87
* ` byte_size(bitstring) `
85
88
* ` div(integer, integer) `
@@ -98,6 +101,9 @@ The Erlang Virtual Machine (VM) only allows a limited set of expressions in guar
98
101
99
102
Additionally, users may define their own guard functions, usually starting with "is_ ".
100
103
104
+ Note that while boolean operators such as ` and ` , ` or ` , ` not ` are allowed in guards,
105
+ the more general and short-circuiting operators ` && ` , ` || ` and ` ! ` are not.
106
+
101
107
Keep in mind errors in guards do not leak but simply make the guard fail:
102
108
103
109
``` iex
0 commit comments