Skip to content

Commit 06f61d6

Browse files
colinleachColin Leach
and
Colin Leach
authored
[Booleans] another attempt (#831)
* another attempt at booleans * Update about.md like that? --------- Co-authored-by: Colin Leach <[email protected]>
1 parent 565e825 commit 06f61d6

File tree

9 files changed

+194
-2
lines changed

9 files changed

+194
-2
lines changed
File renamed without changes.
File renamed without changes.

concepts/booleans/.meta/config.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"authors": [
3+
"colinleach"
4+
],
5+
"contributors": [
6+
"BNAndras"
7+
],
8+
"blurb": "Julia has boolean values true and false, operators ! (not), && (and), || (or)."
9+
}

concepts/booleans/about.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

concepts/booleans/introduction.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Introduction
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+
16+
## Boolean Operators
17+
18+
There are three Boolean operators in Julia.
19+
20+
`&&` is Boolean "and".
21+
It evaluates to `true` if the expressions on *both* sides of `&&` are `true`.
22+
23+
```julia-repl
24+
julia> true && true
25+
true
26+
27+
julia> true && false
28+
false
29+
```
30+
31+
`||` is Boolean "or".
32+
It evaluates to `true` if an expression on *either* side of `||` is `true`.
33+
34+
```julia-repl
35+
julia> true || true
36+
true
37+
38+
julia> false || true
39+
true
40+
```
41+
42+
`!` is Boolean "not".
43+
It exchanges `true` and `false` values.
44+
45+
```julia-repl
46+
julia> !true
47+
false
48+
49+
julia> !false
50+
true
51+
```
52+
53+
For longer and more complicated expressions, it is best to use parentheses to make your intention clear.
54+
55+
```julia-repl
56+
julia> (true || false) && (false && true)
57+
false
58+
```

concepts/booleans/links.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"url": "https://docs.julialang.org/en/v1/manual/mathematical-operations/#Boolean-Operators",
4+
"description": "Boolean Operators, section in the manual"
5+
}
6+
]

config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,8 +1091,8 @@
10911091
"concepts": [
10921092
{
10931093
"uuid": "32384ed4-ef26-4118-8ea3-4d44c331e828",
1094-
"slug": "functions",
1095-
"name": "Functions"
1094+
"slug": "booleans",
1095+
"name": "Booleans"
10961096
},
10971097
{
10981098
"uuid": "006ebce8-87cd-4695-87e6-8a7b8dc2f239",

0 commit comments

Comments
 (0)