The title is be misleading, so please read entire question :-).
By "compound assignment operator" I have in mind a construct like this op=
, for example +=
. Pure assignment operator (=
) does not belong to my question.
By "why" I don't mean an opinion, but resource (book, article, etc) when some of the designers, or their coworkers, etc. express their reasoning (i.e. the source of the design choice).
I am puzzled by asymmetry found in C++ and C# (yes, I know C# is not C++ 2.0) -- in C++ you would overload operator +=
and then almost automatically write appropriate +
operator relying on previously defined operator. In C# it is in reverse -- you overload +
and +=
is synthesised for you.
If I am not mistaken the later approach kills a chance for optimization in case of actual +=
, because new object has to be created. So there has to be some big advantage of such approach, but MSDN is too shy to tell about it.
And I wonder what that advantage is -- so if you spotted explanation in some C# book, tech-talk video, blog entry, I will be grateful for the reference.
The closest thing I found is a comment on Eric Lippert blog Why are overloaded operators always static in C#? by Tom Brown. If the static overloading was decided first it simply dictates which operators can be overloaded for structs. This further dictates what can be overloaded for classes.
1 Answer 1
I can't find the reference for this, but my understanding was that the C# team wanted to provide a sane subset of operator overloading. At the time, operator overloading had a bad rap. People asserted that it obfuscated code, and could only be used for evil. By the time C# was being designed, Java had shown us that no operator overloading was kind of annoying.
So C# wanted to balance operator overloading so that it was more difficult to do evil, but you could make nice things too. Changing the semantics of assignment was one of those things that was deemed always evil. By allowing +=
and its kin to be overloaded, it would allow that sort of thing. For example, if +=
mutated the reference rather than making a new one, it wouldn't follow the expected semantics, leading to bugs.
-
Thank you, if you don't mind I will keep this question open bit longer, and if nobody beat you with the reasoning of actual design, I will close it with accepting yours. Once again -- thank you.greenoldman– greenoldman2015年05月30日 18:29:28 +00:00Commented May 30, 2015 at 18:29
-
@greenoldman - as well you should. I too hope someone come with a answer with more concrete references.Telastyn– Telastyn2015年05月30日 20:04:39 +00:00Commented May 30, 2015 at 20:04
-
So, this is a place where the impossibility of talking both about the pointer and the pointee comfortably and unambiguously rears it's head? Damn shame.Deduplicator– Deduplicator2015年05月30日 21:15:11 +00:00Commented May 30, 2015 at 21:15
-
"People asserted that it obfuscated code" - it's still true though, have you seen some of the F# libraries? Operator noise. E.g. quanttec.com/fparsecDen– Den2015年06月03日 09:39:51 +00:00Commented Jun 3, 2015 at 9:39
+=
first seems absurd; why would you overload a combined operation rather than the parts of it?*=
mutating a reference type to be semantically incorrect.