From: Chris Date: 2002-12-01T06:22:27+09:00 Subject: Re: Ruby ++, the one element and generators The problem is simple: Q: Why isn't "x++" just syntax sugar for "x += 1"? A: Because in C, for example, "x++" doesn't mean "x += 1"! int x, y, z; x = 5; y = x++; // y == 5 x = 5; z = x += 1; // z == 6 It would confuse and upset C programmers, so best not to put it in. Remember, the following code could only be intended to mean two things: x = 5 x++ Either you want "++" to be a method, so you really meant so call the "++" method of the object 5; or you want "++" to be syntax sugar, which we have already seen is a bad idea (confusing and counter-intuitive). However, what does it mean to call 5's "++" method? Again, there are only two reasonable things you could mean: either you want the method to return 6, or you want 5 to change it's value to 6. I thing we would all agree that the latter is absurd (is anyone arguing that they want 5 == 6 ??), so that leaves the former. Let's say we could define it like so: class Fixnum def ++ self+1 end end x = 5 y = x++ # So y == 6, which is 'wrong' from a C standpoint. x # And x is still 5. Terrible. ----- Original Message ----- [snip] In other words, ++ is an operator that replaces a reference to one object with a reference to the next object. This operation may or may not be an inplace operation. ---------------------------- It *cannot* be in-place, because then 5 would equal 6. This is BAD. ---------------------------- In algebraic terms, there is no 'one' element for Real numbers ---------------------------- Uh, where did you get that idea? The real number are a field. Check out your field axioms again. There's an axiom there just to state the existence of '1'. (This doesn't seem relevant to your argument or mine... :) but I had to point that out.) ---------------------------- No we could equally well write a++ as a synonym for a = a.next ---------------------------- No, we couldn't! The only reason anyone wants a "++" operator is because they like it from C/C++/Java/etc. However, if it didn't work like it does in other languages, it would be terrible! We'd never hear the end of it! (Hmm... :) ) Like I said, if we can't implement it *exactly* like it works in C, then it's a bad idea. Surely no one disagrees with this?! "++" is a relic from a language working too close to the processor instructions. Chris