From: Mark Hubbart Date: 2004-04-24T13:27:06+09:00 Subject: Re: Creating bang methods On Apr 23, 2004, at 2:37 PM, Jean-Hugues ROBERT wrote: > At 05:52 24/04/2004 +0900, Jim Weirich wrote: > >> Jean-Hugues ROBERT said: >> > About ++ ... >> > At 03:48 24/04/2004 +0900, you wrote: >> >>Just like +=, ++ can not be defined as a method on an object. >> > >> > but + can be redefined and ++ could as well. >> >> + is a method. It is sent to an object. (e.g. 1+2 === 1.+(2) ) >> >> = is not a method. It is never sent to an object. It binds objects >> to >> names within a scope. (e.g. x=1 means that the name "x" is now >> bound >> to the value "1") >> >> ++ cannot be a method because it changes bindings, just like =. > Let me rephrase. > If ruby was to understand: > this++ > ++that > as (syntax sugar for): > (tmp = this, this = something.postplusplus(), tmp) > (that = that.preplusplus()) > then one could redefine methods postplusplus() and preplusplus() > (whose default definitions should be: self.plusplus(); in a decent > world). > > I understand that = is not a method as of today. And need not to > be for ++ to be re definable (as rephrased). IMHO, *if* it was decided to add ++ and -- to the language, it should be made obvious that ++ is not a method. Instead of translating var++ to var += 1, or defining a #++() method, it should replace the contents of var with var.succ, or var.pred. It might translate something like this: print var++ print var-- print ++var print --var becomes the equivalent of: print lambda{|tmp|tmp=var;var=var.succ;tmp}[nil] print lambda{|tmp|tmp=var;var=var.pred;tmp}[nil] print (var = var.succ) print (var = var.pred) Then all that would be needed would be to implement #succ and #pred for any object, and you can ++ and -- your heart out. It should be pointed out out that var++ is *not* equivalent to var += 1, as someone suggested. var++ returns the original value of var, then sets it to the incremented value. > BTW: Making = a method (of class Binding I guess) would open > interesting perspectives... ;-) !!! I *really* hope that was a joke! :) cheers, --Mark