In every language that I saw that supports the + = operator, the compiler once evaluates the left-side operand to get some type of address, which is then used both to read the old value and write the new one. The + = operator is not just syntactic sugar; as you noticed, it can get the semantics of the expression, which will be inconvenient to achieve using other means.
By the way, the "C" operators in vb.net and Pascal have a similar function. A statement like:
'Assime Foo is an array of some type of structure, Bar is a function, and Boz is a variable.
With Foo (Bar (Boz))
.Fnord = 9
.Quack = 10
End with
will calculate the address Foo (Bar (Boz)), and then set the two fields of this structure to nine and ten. That would be equivalent in C to
{
FOOTYPE * tmp = Foo (Bar (Boz));
tmp-> Fnord = 9;
tmp-> Quack = 10;
}
but vb.net and Pascal do not set a temporary pointer. Although VB.net can achieve the same effect without using āCā to hold down the result of Bar (), using āCā avoids the temporary variable.
supercat
source share