On Tue, Oct 02, 2007 at 12:29:37AM +0900, Robert K. wrote:
- C++ R. type is a special beast which has some additional properties
compared to 1. This does not only allow to reference a value but in
fact you can say that it references a reference[1]. Or you can say
that a C++ reference is an alias for a variable. Hence it allows to
manipulate a variable in a calling scope.
A reference to another reference is explicitly disallowed in C++;
attempt to do this usually result in a reference to the original object.
The implication of this is that you cannot change a reference once it is
created; you can only change the object to which it refers.
As you point out, variable alias is an apt description of a C++
reference.
References in Ruby are IMO very similar to references in C++, with the
exception of assignment semantics. Assignment on a reference in C++
calls the referred object’s assignment operator, while assignment on a
reference in Ruby reassigns the reference.
Ruby’s evaluation strategy is actually “call by reference” although
the description at
Evaluation strategy - Wikipedia does
not cover Ruby properly - unless you interpret “temporary object” as
“copy of a reference” because this is how it works.
This description sounds very C+±centric to me. In particular, I’ve
never heard Lvalue used as part of the definition of call-by-reference
(the discussion page seems to back me up on this a little). I think
foldoc’s definition is better:
call-by-reference from FOLDOC
call-by-reference
An argument passing convention where the address of an
argument variable is passed to a function or procedure, as opposed to
passing the value of the argument expression. Execution of the
function or procedure may have side-effects on the actual argument as
seen by the caller. The C language’s “&” (address of) and “*”
(dereference) operators allow the programmer to code explicit
call-by-reference. Other languages provide special syntax to declare
reference arguments (e.g. ALGOL 60).
Under strict interpretation of this definition I think Ruby is not
call-by-reference, since the address of a variable is never passed, but
rather the object to which the variable refers. Strictly speaking, Ruby
is call by value (of the variable), but since this isn’t very
descriptive of how Ruby actually works, call by object reference sounds
better to me.
I think the muddiness comes from the terms not being descriptive enough
in their naming; they don’t describe in their names what entity is being
passed by value or reference (be it object, variable, or otherwise).
The call by object reference suggestion addresses this issue perfectly.
Also note that the C2 wiki gives a third definition of
call-by-reference:
http://c2.com/cgi/wiki?CallByReference
Paul