From: Lars Christensen Date: 2001-08-24T05:38:25+09:00 Subject: [ruby-talk:20207] Re: ++ Operator On Fri, 24 Aug 2001, Yukihiro Matsumoto wrote: > "++" in C/C++ is fundamentally an assignment, or in other words, an > operation on variable not object, so that it would not fit well in > object-oriented model. Try "i+=1" instead. I don't think of ++ as an assignment, as much as a "modification" to what it is used one. Having written much C and C++ code, the +, += and "missing" ++ operators semantics in Ruby are hard for me to accept. This is how I would have done it: --- += should be a method, not an operator. It modifies the object using addition or appending. Lot of methods in ruby already modify their object. a = b = 2 a += 2 a # => 4 b # => 4 + is a method defined in terms of +=, so that "a = b + c" is equivalent to "a = b.dup; a += c". In case of string appending or even Numeric addition, a more efficient implementation can be made by allowing + to be overloaded (like C++). ++a and a++ are also operators perhaps mapped to methods such as Numeric#++ and Numeric#++@, and having C semantics. They also modify the object (and return the value after or before). a = b = 3 a++ a #=> 4 b #=> 4 The same for --a and a--. --- Note that a += 1 today involves making duplicate of A, adding 1 to it and return a reference to the new object, where as a++ would map directly to the same instruction in C. No objects copied, created or deleted! a++ is "the efficient way" in my mind. Making "a++" an alias for "a += 1" or "a = a + 1" wouldn't make it any better. It has to be the operation that increases the referenced object (Numeric) by one. This may be a drastic change, but I would vote for it. This would be much more natural in my C mind than current Ruby. Methods that modify the state of the object are even common in Ruby and are indeed "object oriented". Avoiding them is "functional thinking" IMO. -- Lars Christensen, larsch@cs.auc.dk