Why no ++ and --?

++ and – modify their receivers. Since Ruby’s Numeric types are immutable,
they cannot be modified. So the only solution is to implement ++ and – as
assignment operators.

Other languages solve similar problems by providing references/
pointers to variable or with macros etc. This isn’t about
immutability.

On 2010-02-16, lith [email protected] wrote:

++ and – modify their receivers. Since Ruby’s Numeric types are immutable,
they cannot be modified. So the only solution is to implement ++ and – as
assignment operators.

Other languages solve similar problems by providing references/
pointers to variable or with macros etc.

I don’t think allowing references or pointers to variables would be
good.
Variables are not objects, they’re references already.

This isn’t about immutability.

In practice it is, because when people say “x++”, they don’t want the
variable
x to refer to a new object, they want the object x refers to increased.

-s

On Tue, Feb 16, 2010 at 6:50 PM, Seebs [email protected] wrote:

In practice it is, because when people say “x++”, they don’t want the variable
x to refer to a new object, they want the object x refers to increased.

Don’t agree with that. For me the contract is just about the variable.
People do not want to be able to do ++5, you can’t do that in
languages that offer ++ either.

On 2010-02-16, Xavier N. [email protected] wrote:

On Tue, Feb 16, 2010 at 6:50 PM, Seebs [email protected] wrote:

In practice it is, because when people say “x++”, they don’t want the variable
x to refer to a new object, they want the object x refers to increased.

Don’t agree with that. For me the contract is just about the variable.
People do not want to be able to do ++5, you can’t do that in
languages that offer ++ either.

In C:

++*p;

Has nothing to do with the variable, has everything to do with the
object
denoted by the expression ‘*p’.

In C, the variable denotes a region of storage. In Ruby, it doesn’t.

-s

On Tue, Feb 16, 2010 at 7:30 PM, Seebs [email protected] wrote:

++*p;

Has nothing to do with the variable, has everything to do with the object
denoted by the expression ‘*p’.

In C, the variable denotes a region of storage. Â In Ruby, it doesn’t.

You could argue that for += as well:

*p += 1;

Does that C use case rule out += in Ruby? Nope we have our own +=
right? Same a posteriori for ++ in my opinion. It could be implemented
given the expectations that are reasonable to have in Ruby.

Sonja Elen K. wrote:

“foo += 1” somehow seems less elegant or pretty as “foo++”.

Is there any reason Ruby doesn’t have ++? Is it something that might be
added in a future version?

foo+=1 follows the POLS.
foo++ leads to tricky behavior and tricky code and “clever” programming,
which is usually a Bad Thing™.

On Tue, Feb 16, 2010 at 11:52 AM, Xavier N. [email protected] wrote:

You could argue that for += as well:

*p += 1;

Does that C use case rule out += in Ruby? Nope we have our own +=
right? Same a posteriori for ++ in my opinion. It could be implemented
given the expectations that are reasonable to have in Ruby.

And += can result in a sequence of message exchanges as well:

obj.meth += 1
obj[member] += 1

These will need to invoke the respective getters and setters for obj, in
addition to invoking ‘+’ on the object returned from the getter.

In that regard += is very much sugar for:

obj.meth = obj.meth + 1