From: Robert Klemme Date: 2013-04-15T18:20:52+09:00 Subject: Re: Wondering why no "increment" or "decrement" operator in ruby --00151757455cb9f77b04da62c4eb Content-Type: text/plain; charset=ISO-8859-1 On Mon, Apr 15, 2013 at 5:38 AM, tamouse mailing lists < tamouse.lists@gmail.com> wrote: > On Sun, Apr 14, 2013 at 10:23 PM, Matthew Kerwin > wrote: > > Incidentally, if you're using MRI, because of a clever optimisation your > > 'a' variable literally holds the value `1`, not a reference per se. > > I guess I don't understand this last part; I can still call an > instance method on a, so it must be more than just a value..., no? As > I can call an instance method on 1. I guess I don't quite get what you > mean by 'value'... > I prefer to look at this on the language level and not the MRI implementation (even though they are closely related). On the language level the optimization is invisible and hence irrelevant for the reasoning here. The reason for the absence of an increment operator is that there was a design decision to make numbers immutable (at least with regard to their numeric value). If instances of a type are immutable their state will never change - hence you cannot change them from representing 1 to 2 etc. Having said that, "++a" could be made syntactic sugar for "a+=1" but that would not work if a was referencing a String even though String#+ is defined. The only way out of this would be to make "++a" syntactic sugar for something like "a+=a.class.one" where Integer would implement "one" as "return 1". But what would String.one return then? Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --00151757455cb9f77b04da62c4eb Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Mon, Apr 15, 2013 at 5:38 AM, tamouse mailing lists <tamo= use.lists@gmail.com> wrote:
On Sun, Apr 14, 2013 at 10= :23 PM, Matthew Kerwin <lists@ru= by-forum.com> wrote:
> Incidentally, if you're using MRI, because of a clever optimisatio= n your
> 'a' variable literally holds the value `1`, not a reference pe= r se.

I guess I don't understand this last part; I can still call an instance method on a, so it must be more than just a value..., no? As
I can call an instance method on 1. I guess I don't quite get what you<= br> mean by 'value'...

I prefer to look at this on the language level and n= ot the MRI implementation (even though they are closely related). =A0On the= language level the optimization is invisible and hence irrelevant for the = reasoning here. =A0The reason for the absence of an increment operator is t= hat there was a design decision to make numbers immutable (at least with re= gard to their numeric value). =A0If instances of a type are immutable their= state will never change - hence you cannot change them from representing 1= to 2 etc.

Having said= that, "++a" could be made syntactic sugar for "a+=3D1"= but that would not work if a was referencing a String even though String#+= is defined. =A0The only way out of this would be to make "++a" s= yntactic sugar for something like "a+=3Da.class.one" where Intege= r would implement "one" as "return 1". =A0But what woul= d String.one return then?

Kind regard= s

robe= rt

--
remember.guy do |as, often| as.yo= u_can - without end
http://blog.rubybestpractice= s.com/
--00151757455cb9f77b04da62c4eb--