The not keyword

When I started programming in Pascal 500 years ago and switched to other languages I always missed the ‘not’ keyword.
Now I see in Zig to my surprise the and / or keywords.
I would really love to see my not back in Zig.
However: in Pascal / Delphi the not/and/or are used for both bitwise operations and booleans, which can be confusing.

The ! is so narrow that it can be easily missed, when scanning code.
A not would be fantastic.

How do you fellow programmers think about this?

2 Likes

Andrew already rejected this: "Not" instead of "!" · Issue #6625 · ziglang/zig · GitHub.

2 Likes

Hahah ok I will read. He should have rejected and/or then as well :slight_smile:

Imo and/or have a good reason not be operator: they have an impact on the control flow. If the left side of an or expression is true, the right side won’t to be evaluated, same when the left side of an and expression is false.

This isn’t the case for the ! operator.

4 Likes

And you should also read this: require parentheses sometimes to disambiguate confusing operator precedence · Issue #114 · ziglang/zig · GitHub

Ah interesting. I will read.
I will miss my beloved not forever.

Sidenote:
When I was reading this topic, just for a moment I struggled to remember what operator Zig is actually using for the “boolean not”. The reason being that I instinctively try to avoid it in order to simplify logic. Since “boolean not” is a logical operation that can be removed without introducing any other logical operations, removing it saves every reader from tracking that redundant negation, meaning it improves readability.

There’re two obvious ways of eliminating it:

  1. Rename boolean variables
var is_false = false;
while (!is_false) {
    ...
}

Becomes:

var is_true = true;
while (is_true) {
    ...
}
  1. Apply “De Morgan’s laws
if (!(a >= b and c >= d)) {
    ...
}

Becomes:

if (a < b or c < d) {
    ...
}
4 Likes

I never use what I call “The Back and Forth Bouncing Booleans” which attacks my brain.

And yes this is a good example! (including the crazy ! for not, who on earth invented that…).

if (!(a >= b and c >= d))

For my (C#) work I sometimes use:

if (MyLookupFunction(args) == false) // this
if (!MyLookupFunction(args)) // instead of this

EDIT: chat gpt answer to who is guilty

The choice of ! for “not” is rooted in early programming languages and mathematics, where brevity was essential due to limited screen space and memory. This notation likely traces back to BCPL (the precursor to C) and ALGOL. Ken Thompson and Dennis Ritchie, while developing C, adopted ! as a logical NOT operator, making it standard for languages that followed.

The irony is real, though—! is indeed narrow and easy to miss, especially given its significance in logical expressions. Unfortunately, by now, it’s deeply embedded in programming conventions.

Sometimes we are just stuck in reality :slight_smile:

It ain’t pretty, but it works :joy_cat:

const std = @import("std");

inline fn not(b: bool) bool {
    return !b;
}

pub fn main() !void {
    if (not(3 > 4)) {
        std.debug.print("{}\n", .{not(3 > 4)});
    }
}
6 Likes

You just invented a new keyword.

1 Like

never saw it like this before, if there is no not keyword but only !,

pub fn main() !void {

is a bit strange; the function does not return not void

1 Like

This is much easier to read.