Why no ++ and --?

  • Thread starter Sonja Elen Kisa
  • Start date
X

Xavier Noria

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.
 
S

Seebs

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
 
X

Xavier Noria

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. =C2=A0In Ruby, it doesn't=
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

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
 
A

Aldric Giacomoni

Sonja said:
"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(tm).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top