No alias for increment/decrement like ++ and - -?

Hello. I have written some bits of C and Pawn in the past and both have the ++ and - - aliased to increment/decrement operations. My question is: why is this not available in zig?

I’d consider myself intermediate-level but i don’t know exactly how these are translated by the compiler, other than it’s something like {type}myvar+=1

because += or -= is more explicit, also the savings between +/-=++/-- are smaller than = a +/- ba +/-= b.

++ in zig is used for array concatination. it is comptime only, doing it at runtime requires dynamic allocation which zig doesnt want to hide.

C’s ++ is just too cursed, imo: it is equivalent to (fully desugared)

_ = blk: {
    const b = a;
    a = a + 1;
    break :blk b;
};

dont forget get about the differences of ++a and a++

++a is a += 1; b = a but a++ is b = a; a += 1;
which leads to a = a++ doing nothing.

1 Like

i have never seen ++i used in production - but i haven’t looked at a lot of stuff tbh.

But you probably have seen a++; b = a;, which is the same thing.

ofc that is such a minor ‘improvement’ both ++a and a++ have less reason to both exist in c, than either one has to be in zig.

it just feels strange since when you learn C/C++ you tend to learn ++ really early

i have never seen ++i used in production - but i haven’t looked at a lot of stuff tbh.

It’s actually used a lot, and it used to be the case that pre-increment produced faster code (I guess as no temporary is needed in any circumstance). I’m sure modern compilers have erradicated that difference, though maybe not in C++ where it can be overloaded.

Also, by not having pre/post increment, we avoid confusing expressions like:

--a * a / b++;

…since += and -= are assignment operators, which are in turn statement and not expressions.

It’s all over the place, as is stuff like blah[i++].

It’s too clever, is what it is. You can change something and reference it at the same time, and unlike more obviously dubious things like blah[ a = b ];, which is legal btw, it’s cute and compact and that encourages using it.

I wrote some C recently and it was so much fun! so many integer / boolean puns, so many things you can write ‘elegantly’ for that special C definition of elegant where you just count the bytes in the source code.

But then comes the day when you have to read it…

2 Likes

this checks out :+1: (in a Pawn project I was working on, i had to unroll some loops for performance reasons previously)

1 Like

Some embrace it hard: here’s a gem found in some snprintf implementations: ++ * --p1; which is a funky way to increment the content of a decremented pointer. A good thing not to have in Zig, yeah? :slight_smile:

2 Likes

ok, that’s a bit nuts xD