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
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.
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?