Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Booleans] new concept #781

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions concepts/booleans/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"authors": [
"colinleach"
],
"contributors": [
"BNAndras"
],
"blurb": "Julia has boolean values true and false, operators ! (not), && (and), || (or)."
}
120 changes: 120 additions & 0 deletions concepts/booleans/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# About

## Booleans in Julia

True or false values are represented by the `Bool` type.
It contains only two values: `true` and `false`.

```julia-repl
julia> true
true

julia> false
false

julia> typeof(true)
Bool
```

In contrast to several other languages, Julia deliberately has no concept of "truthiness".
Only expressions which evaluate to `true` or `false` will be treated as a `Bool`.

Specifically, empty arrays or strings will *not* be interpreted as `false`.
There must be an appropriate test such as `isempty()` if you want special handlind for empty values.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
There must be an appropriate test such as `isempty()` if you want special handlind for empty values.
There must be an appropriate test such as `isempty()` if you want special handling for empty values.


## Boolean operators

There are three [Boolean operators][boolean-operators] in Julia.

`&&` is Boolean "and".
It evaluates to `true` if the expressions on *both* sides of `&&` are `true`.

```julia-repl
julia> true && true
true

julia> true && false
false
```

`||` is Boolean "or".
It evaluates to `true` if an expression on *either* side of `||` is `true`.

```julia-repl
julia> true || true
true

julia> false || true
true
```

`!` is Boolean "not".
It exchanges (inverts) `true` and `false` values.

```julia-repl
julia> !true
false

julia> !false
true
```

These operators will be familiar to users of many other languages, though *not* Python.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence is confusing. and and or exist in other languages. Perhaps we say the operators and these concepts should be familiar coming from other languages but they might have different names there.


## Operator precedence

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)*.

Relying on this can be confusing and error-prone.
For clarity, use parentheses to make your intention clear.

## Short-circuit evaluation

Does the expression `true || x` depend on the value of `x`, or can the compiler ignore `x`?

Julia evaluates Boolean expressions from left to right, and stops when it has an unambiguous result.

For example, `true || x` must be `true`, regardless of `x`, so `x` is not evaluated.
Similarly, `false && y` is `false`, with no need to evaluate `y`.

Conversely, `true && x` is `true` *only if* `x` is `true`, so `x` must be evaluated.

Similarly, if we chain multiple operators:

```julia-repl
julia> true && false && something_else
false
```

Because `true && false` must be `false`, the `something_else` is unimportant and is ignored by the compiler.

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)*.

Such short-circuit evaluation is quite often used by Julia programmers as a shortcut to trap runtime problems and edge cases:

```julia
all_ok || do_something()

is_problem && do_something_else()
```

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.

This is a slight abuse of Boolean syntax, but it can be very convenient.

## How Bools work internally

If a Bool is included in an *arithmetic* expression, `true` is interpreted as `1` and `false` as `0`, reflecting how they are stored.

If you are used to lower-level languages (C and similar), *please* avoid using this often.
It will reduce code readability and make debugging harder.

You may sometimes see the numerical values used as a quick way to count how many things are `true`.

```julia-repl
julia> true + false + true
2
```

[operator-precedence]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity
[boolean-operators]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Boolean-Operators
58 changes: 58 additions & 0 deletions concepts/booleans/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Introduction

## Booleans in Julia

True or false values are represented by the `Bool` type.
It contains only two values: `true` and `false`.

```julia-repl
julia> true
true

julia> false
false
```

## Boolean Operators

There are three Boolean operators in Julia.

`&&` is Boolean "and".
It evaluates to `true` if the expressions on *both* sides of `&&` are `true`.

```julia-repl
julia> true && true
true

julia> true && false
false
```

`||` is Boolean "or".
It evaluates to `true` if an expression on *either* side of `||` is `true`.

```julia-repl
julia> true || true
true

julia> false || true
true
```

`!` is Boolean "not".
It exchanges `true` and `false` values.

```julia-repl
julia> !true
false

julia> !false
true
```

For longer and more complicated expressions, it is best to use parentheses to make your intention clear.

```julia-repl
julia> (true || false) && (false && true)
false
```
6 changes: 6 additions & 0 deletions concepts/booleans/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"url": "https://docs.julialang.org/en/v1/manual/mathematical-operations/#Boolean-Operators",
"description": "Boolean Operators, section in the manual"
}
]
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,11 @@
"slug": "functions",
"name": "Functions"
},
{
"uuid": "813fb2fa-0068-4167-93d6-e8b8e7b7b55c",
"slug": "booleans",
"name": "Booleans"
},
{
"uuid": "006ebce8-87cd-4695-87e6-8a7b8dc2f239",
"slug": "integer-introduction",
Expand Down