From simon.urbanek at r-project.org  Fri Jan 10 03:44:10 2014
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Thu, 9 Jan 2014 21:44:10 -0500
Subject: [Rd] reference class internals
In-Reply-To: <20140109232017.GA6114@laura>
References: <20140109223322.GB5772@laura>
	<CABdHhvGVn4j2osVs-UK2sU1fFa4LyXMB-uDzBwYJWNZ+dNxtoA@mail.gmail.com>
	<20140109232017.GA6114@laura>
Message-ID: <14D88F33-31D2-47FB-8224-3C2A9FD49CC2@r-project.org>

On Jan 9, 2014, at 6:20 PM, Norm Matloff <matloff at cs.ucdavis.edu> wrote:

> Bottom line:  Really no different from the case of ordinary vectors that are not in reference classes, right?  In other words, not true pass-by-reference.
> 

The pass-by-reference applies to the object itself, not necessarily to anything you obtain by calling a function on the object (like extracting a part from it). Vectors are not reference-semantics objects so regular rules apply.

If you pass a reference semantics object to a function, the function can modify the object. If you pass any other object, the contents are guaranteed to not be touched. Reference-semantics objects in R are literally passed by reference (same C pointer), so yes, it is true pass-by-reference.

Cheers,
Simon


(*) - technically, there is a thin non-refernce wrapper around the instances of reference classes, because there are things you don't want to happen to your ref-semantics instance - e.g. you don't want unclass(x) to destroy x and all instances of it (which it would do if there was no wrapper). But the actual payload of the object is a true ref-semantics object - an environment - that is always passed by reference.



> Norm
> 
> On Thu, Jan 09, 2014 at 04:43:44PM -0600, Hadley Wickham wrote:
>> It's a bit of a simplification, reference classes are wrappers around
>> environments.  So if modifying a value in an environment would create
>> a copy, then modifying the same value in a reference class will also
>> create a copy.
>> 
>> The situation with modifying a vector is a bit complicated as it will
>> sometimes be modified in place and sometimes be duplicated and
>> modified (depending on whether its NAMED attribute is 1 or 2, and
>> exactly how you're modifying it).
>> 
>> Hadley
>> 
>> On Thu, Jan 9, 2014 at 4:33 PM, Norm Matloff <matloff at cs.ucdavis.edu> wrote:
>>> I have a question about reference classes, which someone here
>>> undoubtedly can answer immediately, saving me hours of wading through
>>> indecipherable internal code. :-)  Thanks in advance.
>>> 
>>> Reference class data is mutable, fine, but in what sense?  Is it really
>>> physical,  or is it just a view given to the programmer?
>>> 
>>> If for instance I have vector as a field in a reference class, and I
>>> change one element of the vector, is it really true that the change is
>>> guaranteed to be made in-place, no copying, no memory reallocation etc?
>>> 
>>> Norm
>>> 
>>> ______________________________________________
>>> R-devel at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>> 
>> 
>> 
>> -- 
>> http://had.co.nz/
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 



[Rd] reference class internals
Simon Urbanek