I've seen this mentioned a few times. =A0I think it's something of a gros= s
misstatement of a problem, and a deeper understanding might be able to pe= el
away some of the layers of the debate here.
I can't think of a language where "variables are objects". =A0And this is= n't
the issue.
In C++ a variable can actually hold the state of an object rather than
simply a reference to the object.
If you think of object as simply meaning what holds the state of what
is denoted by a variable, then C variables hold objects, and since a
pointer represents state, even pointer's can be considered objects in
this degenerate (if you will) form.
And that really is the point with the ++ operator, at least if you
want it to have the semantics of C/C++.
the c/c++ expression
b =3D a++
has the meaning.
1) copy the current contents of the a variable to b
2) increment the contents of the variable a according to the static
type of a, e.g. if it's an int or a pointer to a char add 1 to it, if
it's a pointer to a (4-byte) int add 4 to it, etc.
The original quest in this thread was to do this by defining a ++
method for FixNum (or Integer).
As Matz himself has pointed out in this thread,
There's no way to modify local variables by sending message in Ruby.
matz.
Which is something I've said on this thread before (multiple times IIRC).
This has nothing to do with whether or not the object bound to a
variable is immutable, it has to do with how ruby variable bindings
can and cannot be changed, and that is the whole point.
Now, while it's true that Fixnums have a property of being represented
by an immediate value rather than a pointer to a 'boxed' value, and
this is possible because they are immutable, it doesn't mean that
local variables with non-immediate bindings can be rebound inside the
invocation of another method.
The only way to define b =3D a++. in Ruby with C/C++ semantics would be
to alter the parser to treat such expressions as syntactic sugar,
(which was Charle's proposal), much like +=3D is handled now.
Let's say this were done, perhaps by the parser treating:
b =3D a++
as if it were
b, a =3D a, a.succ
much like it treats
a +=3D 1
as if it were
a =3D a + 1
The actual parser changes would need to be sensitive to other cases like
foo(b + a++)
which would need to compute a + b before incrementing a.
One could alter what the increment did, by redefining succ or a
particular class, much like redefining + for a given class can alter
the value of a + 1 for a particular binding of a.
But one could still not move the actual change of binding into a
method, any more than you can redefine the '=3D' part of '+=3D'
--=20
Rick DeNatale
Blog:
http://talklikeaduck.denhaven2.com/
Twitter:
http://twitter.com/RickDeNatale
WWR:
http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn:
http://www.linkedin.com/in/rickdenatale