Skip to content

Commit f2d388f

Browse files
committed
functions/macros allowed in guard revised
Corrected and additional explanations added.
1 parent d14f2cd commit f2d388f

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

Diff for: getting-started/case-cond-and-if.markdown

+13-7
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ The first clause above will only match when `x` is positive.
5454

5555
## Expressions in guard clauses
5656

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`, `>>>`)
6466
* all the following type check functions:
6567
* `is_atom/1`
6668
* `is_binary/1`
@@ -80,6 +82,7 @@ The Erlang Virtual Machine (VM) only allows a limited set of expressions in guar
8082
* `is_tuple/1`
8183
* plus these functions:
8284
* `abs(number)`
85+
* `binary_part(binary, start, length)`
8386
* `bit_size(bitstring)`
8487
* `byte_size(bitstring)`
8588
* `div(integer, integer)`
@@ -98,6 +101,9 @@ The Erlang Virtual Machine (VM) only allows a limited set of expressions in guar
98101

99102
Additionally, users may define their own guard functions, usually starting with "is_".
100103

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+
101107
Keep in mind errors in guards do not leak but simply make the guard fail:
102108

103109
```iex

0 commit comments

Comments
 (0)