Skip to content
Merged
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
26 changes: 26 additions & 0 deletions lang-guide/chapters/types/basic_types/bool.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,29 @@ The following commands take a closure (or a row condition) as their main argumen
- `in`
- Regex comparison operators: `=~`, `!~` `<regex>`
- String comparison operators: `ends-with`, `starts-with`

## Short-circuit evaluation

`and` and `or` short-circuit: the right-hand operand is only evaluated when the left-hand operand doesn't already decide the result. With `and`, the right-hand side is skipped when the left-hand side is `false`; with `or`, it is skipped when the left-hand side is `true`.

```nu
def check [] { print "check was called"; true }
false and (check)
# => false
true or (check)
# => true
true and (check)
# => check was called
# => true
```

This means a condition can guard an expression that would otherwise fail:

```nu
let text = ""
($text != "") and (($text | split chars | last) == "%")
# => false
```
Loading