This post is about a bugtracker issue I've just read,
https://bugs.freepascal.org/view.php?id=39206.
It seems that the shorthand assignment operators +=, -=, etc. in FPC have quite different semantics of that in C/C++ and are simply evaluated as a += b <=> a := a + b.
From the Reference manual:
13.1.1 Assignments
...
Remark These constructions are just for typing convenience, they don’t generate different code.
At the other hand, for C/C++:
https://en.cppreference.com/w/cpp/language/operator_assignmentThe behavior of every builtin compound-assignment expression E1 op= E2 (where E1 is a modifiable lvalue expression and E2 is an rvalue expression or a braced-init-list (since C++11)) is exactly the same as the behavior of the expression E1 = E1 op E2, except that the expression E1 is evaluated only once and that it behaves as a single operation with respect to indeterminately-sequenced function calls.
And for C#:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/assignment-operatorFor a binary operator op, a compound assignment expression of the form
x op= y
is equivalent to
x = x op y
except that x is only evaluated once.
As a bilingual programmer, I find this quite misleading (see the bugtracker issue). Assignments like
a[f()] += b; will evaluate the left-hand expression twice thus doubling f()'s side effects if any.
In C/C++ very common practice is to write things like:
Personally, I'm avoiding the use of any C-style tricks in my Pascal sources just to maintain a clear distinction between them and the C's. My question actually is why a C-like feature was introduced in FPC but not with the same semantics? Anybody has an opinion? May be the compiler guys?