[Rd] reference class internals
simon.urbanek at r-project.org
Fri Jan 10 03:44:10 CET 2014
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.
(*) - 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.
> 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).
>> 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?
>>> R-devel at r-project.org mailing list
> R-devel at r-project.org mailing list
More information about the R-devel