From: Christopher Dicely Date: 2009-11-01T03:02:32+09:00 Subject: Re: Ruby doesn't implement x++ for Fixnum's because ??? On Fri, Oct 30, 2009 at 3:20 PM, Robert Klemme wrote: > On 10/30/2009 08:03 PM, Rick DeNatale wrote: >> >> On Fri, Oct 30, 2009 at 11:05 AM, Robert Klemme >> wrote: >>> >>> 2009/10/30 RichardOnRails : >>> >>>> I'll correct my errors and probably have a new theory subsequently. >>> >>> IMHO the story goes like this: absence of postfix ++ and -- is a >>> consequence of the fact that numeric types are immutable in Ruby which >>> makes an assignment necessary for these operators.  Although that >>> would be doable, it would not immediately be obvious when looking at >>> "foo++".  On the other side, "foo += 1" makes the assignment obvious >>> while still being pretty concise (you do not have to write "foo = foo >>> + 1"). >> >> I think that theres a more fundamental problem with ++ in a language >> like ruby, which has to do with the difference between objects and >> variables. >> >> The c ++ and -- operators change a variable, NOT a value. > > 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. I think it would make more sense, if you were going to define a "++" syntax for Ruby, to use "a.succ" rather than "a+1", and if you are going to create syntax which does an assignment but returns the pre-assignment value of the variable receiving the assignment, I'd go further and include a more general form for that, say ":=", such that "a := b" is equivalent to "(_ = a),(a = b), _" and then define postfix "++" such that "a++" is "a := a.succ" and prefix "++" such that "++a" is equivalent to "a = a.succ".