On Sat, Feb 13, 2010 at 8:48 PM, Josh Cheek <josh.cheek / gmail.com> wrote:

> I think that people who ask for ++ really don't understand what they are
> asking for, and think it is just a simple syntactic difference.
>

Sorry to flay the dead horse's sun-bleached bones a bit more, but...

++ and -- (both prefix and postfix), were they to be added to Ruby, should
be implemented as little more than syntactic sugar.  Much of the opposition
to ++ and -- comes from the idea that they *must* be implemented as methods
instead of as syntactic sugar as I've related previously in this post.  It's
certainly internally consistent to implement all operators as methods, and
that's great, but Ruby doesn't implement "=" (or any assignment operators
like +=, *= etc) as a method.

Certain combinations of operations like obj[x] += 1 may involve complex
sequence of message dispatches.  This operation sends the [] message to obj
with x as a parameter, then invokes the "+" method on the result of obj[x],
then invokes the []= method on obj with x and the result of the + operation.

So, not to toss any lighter fluid onto the horse's bleached bones, but I
think those opposed to "++" and "--" operators possess an intermediate
understanding of Ruby and don't know that down there in the trenches of Ruby
implementation, things are already a bit ugly.  A single operator alone may
invoke multiple methods, and combinations thereof may permute that further,
particularly when assignment is involved.  Operators ARE NOT directly
equivalent to method calls in all cases.

In a language like Ruby, if you were to introduce "++" and "--" they MUST be
classed as assignment operators; otherwise they don't make sense.  Much of
the opposition to them seems to center around the "what if ++ and -- aren't
assignment operators?" argument which is certainly a dead horse.  I freely
admit that trying to implement ++ or -- as anything but compound assignment
operators doesn't make any sense whatsoever.

Implementing these operators as syntactic sugar for something like this for
x++:

   begin; _tmp = x; x += 1; _tmp; end

...is the only sensible solution, and anything else is a red herring.

-- 
Tony Arcieri
Medioh! A Kudelski Brand