From: Seebs Date: 2009-11-09T22:25:04+09:00 Subject: Re: Ruby doesn't implement x++ for Fixnum's because ??? On 2009-11-09, Joel VanderWerf wrote: > Does > > foo {x++} > > yield the incremented x to foo, or the original x? What if foo yields > several times? Increment once, or once per iteration? Postincrement should always produce the original value. > What about > > x = 0 > y = case; when true; x++; end > > Is y 0 or 1? 0, of course. postincrement gives the original value. I would say that, by the end of the assignment to y, x has taken the value 1, but what's yielded by a postincrement is not the value of x, but a separate value which happens to be the same as the value x used to have. > It's not an answer to say > > 'x++' should have the same effect as 'x+=1' > > because that's not even true in C. Right. But that is (nearly always) what "++x" should mean. Hmm. Let us imagine that we were to make ++ a rebinding operator, like +=. Not just for Numerics, but always -- otherwise we lose the expected semantics. Hmm. This leads to an interesting point: Since you can't modify the underlying object when it's immutable, it violates POLS for ++ to ever modify the underlying object. It would ALWAYS be a rebinding to a new value. The semantics of "++x" would indeed be exactly equivalent to "x += 1". The semantics of "x++" would be equivalent to magic_temporary_value = x x += 1 magic_temporary_value The question is... Would this be useful enough to justify the hassle? I'm not sold on it. The postincrement form is really primarily useful for idioms that don't make any sense in Ruby. The preincrement form is actually sorta nice, because it really is easier for me to read (I don't have to actually think about whether the amount incremented by is really 1 or not). I'm not sure it's nice enough to justify the effort. -s -- Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!