From: Rick DeNatale Date: 2009-11-01T06:19:01+09:00 Subject: Re: Ruby doesn't implement x++ for Fixnum's because ??? On Fri, Oct 30, 2009 at 6:20 PM, Robert Klemme wrote: > On 10/30/2009 08:03 PM, Rick DeNatale wrote: > How their behavior is defined is completely up to the language designer. >  You could make "a++" syntactic sugar for > > (_tmp = a; a = a + 1; _tmp) > > in the same way as "a += 1" is syntactic sugar for "a = a + 1" or "a ||= 10" > is syntactic sugar for "a || a = 10".  In fact the sequence in brackets > above would be a completely reasonable way to do it in Ruby: you could use > "a++" in a similar way as in C++ and Java without sacrificing Fixnum's > immutability. And I would argue that you still can't implement ++ for fixnum since there isn't a ++ method, any more than there is a += method. > But: you would hide the assignment in the syntactic sugared version and this > could lead to unexpected behavior, like Which I think gets to the fundamental difference. In C/C++ an integer variable is just a bit-string of a particular length interpreted as a signed (probably 2s complement) integer, and ++ is a operator on the variable which twiddles those bits. In Ruby and languages of its ilk, a variable is a reference to an object, and can only be modified by an assignment, not by a method of the object it happens to be referencing at the moment, whether that object is mutable or not. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale