|
| 1 | +# About |
| 2 | + |
| 3 | +## Booleans in Julia |
| 4 | + |
| 5 | +True or false values are represented by the `Bool` type. |
| 6 | +It contains only two values: `true` and `false`. |
| 7 | + |
| 8 | +```julia-repl |
| 9 | +julia> true |
| 10 | +true |
| 11 | +
|
| 12 | +julia> false |
| 13 | +false |
| 14 | +
|
| 15 | +julia> typeof(true) |
| 16 | +Bool |
| 17 | +``` |
| 18 | + |
| 19 | +In contrast to several other languages, Julia deliberately has no concept of "truthiness", and only expressions which evaluate to `true` or `false` will be treated as a `Bool`. |
| 20 | + |
| 21 | +Specifically, empty arrays or strings will *not* be interpreted as `false`. |
| 22 | +There must be an appropriate test such as `isempty()` if you want special handling for empty values. |
| 23 | + |
| 24 | +## Boolean operators |
| 25 | + |
| 26 | +There are three [Boolean operators][boolean-operators] in Julia. |
| 27 | + |
| 28 | +`&&` is Boolean "and". |
| 29 | +It evaluates to `true` if the expressions on *both* sides of `&&` are `true`. |
| 30 | + |
| 31 | +```julia-repl |
| 32 | +julia> true && true |
| 33 | +true |
| 34 | +
|
| 35 | +julia> true && false |
| 36 | +false |
| 37 | +``` |
| 38 | + |
| 39 | +`||` is Boolean "or". |
| 40 | +It evaluates to `true` if an expression on *either* side of `||` is `true`. |
| 41 | + |
| 42 | +```julia-repl |
| 43 | +julia> true || true |
| 44 | +true |
| 45 | +
|
| 46 | +julia> false || true |
| 47 | +true |
| 48 | +``` |
| 49 | + |
| 50 | +`!` is Boolean "not". |
| 51 | +It exchanges (inverts) `true` and `false` values. |
| 52 | + |
| 53 | +```julia-repl |
| 54 | +julia> !true |
| 55 | +false |
| 56 | +
|
| 57 | +julia> !false |
| 58 | +true |
| 59 | +``` |
| 60 | + |
| 61 | +Similar operators will be conceptually familiar to users of many other languages, though the precise syntax may vary between languages. |
| 62 | + |
| 63 | +## Operator precedence |
| 64 | + |
| 65 | +In more complex expressions, it can be useful to know that `&&` has a slightly higher [precedence][operator-precedence] than `||` *(in the same way that `*` is applied before `+` in arithmetic expressions)*. |
| 66 | + |
| 67 | +Relying on this can be confusing and error-prone. |
| 68 | +For clarity, use parentheses to make your intention clear. |
| 69 | + |
| 70 | +## Short-circuit evaluation |
| 71 | + |
| 72 | +Does the expression `true || x` depend on the value of `x`, or can the compiler ignore `x`? |
| 73 | + |
| 74 | +Julia evaluates Boolean expressions from left to right, and stops when it has an unambiguous result. |
| 75 | + |
| 76 | +For example, `true || x` must be `true`, regardless of `x`, so `x` is not evaluated. |
| 77 | +Similarly, `false && y` is `false`, with no need to evaluate `y`. |
| 78 | + |
| 79 | +Conversely, `true && x` is `true` *only if* `x` is `true`, so `x` must be evaluated. |
| 80 | + |
| 81 | +Similarly, if we chain multiple operators: |
| 82 | + |
| 83 | +```julia-repl |
| 84 | +julia> true && false && something_else |
| 85 | +false |
| 86 | +``` |
| 87 | + |
| 88 | +Because `true && false` must be `false`, the `something_else` is unimportant and is ignored by the compiler. |
| 89 | + |
| 90 | +In this case, `something_else` did not exist as a variable, but including it in this context gave no error *(test this in the REPL if you doubt it)*. |
| 91 | + |
| 92 | +Such short-circuit evaluation is quite often used by Julia programmers as a shortcut to trap runtime problems and edge cases: |
| 93 | + |
| 94 | +```julia |
| 95 | +all_ok || do_something() |
| 96 | + |
| 97 | +is_problem && do_something_else() |
| 98 | +``` |
| 99 | + |
| 100 | +For example, the `do_something` might be an early `return` from the function if `all_ok` is `false`, or assigning a default value to a variable before continuing. |
| 101 | + |
| 102 | +This is a slight abuse of Boolean syntax, but it can be very convenient. |
| 103 | + |
| 104 | +## How Bools work internally |
| 105 | + |
| 106 | +If a Bool is included in an *arithmetic* expression, `true` is interpreted as `1` and `false` as `0`, reflecting how they are stored. |
| 107 | + |
| 108 | +If you are used to lower-level languages (C and similar), *please* avoid using this often. |
| 109 | +It will reduce code readability and make debugging harder. |
| 110 | + |
| 111 | +You may sometimes see the numerical values used as a quick way to count how many things are `true`. |
| 112 | + |
| 113 | +```julia-repl |
| 114 | +julia> true + false + true |
| 115 | +2 |
| 116 | +``` |
| 117 | + |
| 118 | +[operator-precedence]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity |
| 119 | +[boolean-operators]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Boolean-Operators |
0 commit comments