From: Brian Candler Date: 2002-11-05T20:18:47+09:00 Subject: Re: Thoughts on Ruby On Mon, Nov 04, 2002 at 10:02:12PM +0900, Gavin Sinclair wrote: (an all-encompassing statement if ever I saw one :-) > a += b is always, by definition, in any language, the same *behaviour* as a = a > + b. If you disagree, you are being absurd. By whose definition? Certainly not the language designers and standards bodies. As a counter-example: in C++, if an object has both "+" and "+=" methods, the language definition does not require them to have the same behaviour, and therefore in general they don't. QED. In those languages, "+=" is just a label ( / token / method name). There may be some sort of shared understanding between certain groups of programmers that "+" and "+=" ought to work in the way you describe, but it's not a "definition". Programmers in certain languages may reasonably assume that a += b modifies the object referred to by 'a' in place, which is not the behaviour you describe. It's just part of the conventions of that particular language. Actually, many languages don't even _have_ a "+=" operator, which also breaks your statement. However I will assume "any language" is replaced with "any language which has these operators" in the above :-) > The only possible difference is > in efficiency: run-time hacks that save an intermediate object. This is a > sometimes a valid concern, and there is a remedy: the << operator. > > a = a + b (old object 'a' lost) > a << b (same behaviour, *possibly* more efficient) > > This behaviour is defined for Strings and Arrays Those two examples do not exhibit the same behaviour in the presence of other variables containing references to the same object as the original 'a'. That's not a "run-time hack", that's a difference in semantics: i.e. "I want to create a new object and leave the old one alone", as opposed to "I want to modify the object itself". The difference is important and not just one of efficiency, unless we know for definite that there are no such additional references. I don't think we should restrict ourselves to Strings and Arrays, since you made a sweeping statement about operators in general. Even if there is a shared understanding of what we mean by "+" or "+=" on a String, it might not be so clear for a Foo object. Furthermore, the "<<" example is meaningless where 'a' references an immutable object; it may calculate some value, but can't modify the object in place. Anyway, I think the point I'm trying to make is: 1. There are two valid ways to apply an operator and argument to an object: create a new object, or modify in place (if it is mutable) 2. You need two different operators to be able to distinguish them 3. Whether they are called "+" and "+=" (as a C++ programmer might use), or "+" and "<<" (as a Ruby programmer would use, since he doesn't have the option of giving different semantics to "+="), or "foo" and "foo!", is pretty irrelevant. It's just convention. To me, coming from a C background, it looks odd that "integer left shift by X" also means by convention "append value X", but you get used to it as part of learning the language. Regards, Brian.