Its not a special syntax for "+1", its a special syntax for the combination of assignment and increment/decrement.
The postfix versions (increment/decrement variable and then return the value that was present before the increment/decrement), particularly, are equivalent to:
_x = x; x += 1; _x
In an environment where that pattern of operations is common, I can see the value of having a concise mechanism for expressing it.OTOH, there's a cost to it, too, and I'm mostly happier without them around.
1 is just another number, the fact that it's useful for enumerating arrays in C isn't relevant as Swift provides more native solutions for that, the use of which should be encouraged.
> Anointing "+ 1" as a thing that needs special syntax is weird.
1 is special.
Why is adding 1 to something special, compared to adding 2 to something? The #1 use case, by far, is C-style for loops, which aren't used in idiomatic Swift code.
For instance, on a 64-bit x86 machine,
ADD RAX,1
is nine bytes long, while INC RAX
is one byte. Maybe not as much a win these days, but back when memory was smaller and more expensive, it was worth it. #include <stdint.h>
uint64_t inc(uint64_t x) { return x + 1; }
With -O0 gives me: 48 83 c0 01 add $0x1,%rax
and with -O2: 48 8d 47 01 lea 0x1(%rdi),%rax
That is, both are four bytes.