From: Robert Klemme Date: 2007-10-05T04:50:04+09:00 Subject: Re: Reference vs. Reference On 04.10.2007 19:45, Paul Brannan wrote: > On Tue, Oct 02, 2007 at 12:29:37AM +0900, Robert Klemme wrote: >> 2. 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 >> http://en.wikipedia.org/wiki/Call_by_reference#Call_by_reference 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: > > http://foldoc.org/?call-by-reference > > 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. Not the object is passed but the address / handle or however you want to name it. If it were the object we'd have call by value IMHO (objects are values in Ruby). > 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. Yep, I've settled with call by object reference. However, I still believe that call by reference is closer than call by value because the latter would imply that copies of objects are passed and so modifications of objects cannot have side effects on the caller. This is a dangerous assumption. :-) > 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). Good point. Side note: I find it extremely interesting how much debate we can have about terms in a subject as formalized as computer science. > The call by object reference suggestion addresses this issue perfectly. Right. > Also note that the C2 wiki gives a third definition of > call-by-reference: > > http://c2.com/cgi/wiki?CallByReference Actually I believe this is not a third definition of call by reference but the same we had before (wikipedia). I'd disregard the comment in italics because IMHO that makes the mistake to use C++ reference semantics here. IMHO the crucial bit about CBR is the aliasing effect, i.e. modifications of the value are visible to the caller. Kind regards robert