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

Add a section on booleans #204

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 chapter-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,32 @@ const Tagged = union(enum) { a: u8, b: f32, c: bool };
const Tagged2 = union(enum) { a: u8, b: f32, c: bool, none };
```

# Boolean Rules

Zig uses the type `bool` for booleans with two values: `true` and `false`.

Logical ands and ors use the `and` and `or` keywords respectively.
These operations short-circut.

Note: logical negation is expressed with `!` (not a keyword).
Furthermore, bitwise operators cannot be used with booleans.

```
test "boolean and and or with short circuits" {
const t = true;
const f = !t;
try expect(t or f);
try expect(t and !f);

// Optionals are covered in more detail later.
// Just know that in this case optional_to_induce_error.? is a runtime error.
const optional_to_induce_error: ?bool = null;
// None of the below cause an error thanks to short-circuiting.
try expect(t or optional_to_induce_error.?);
try expect(!(f and optional_to_induce_error.?));
}
```

# Integer Rules

Zig supports hex, octal and binary integer literals.
Expand Down