From ripley at stats.ox.ac.uk  Tue Nov 13 21:05:16 2007
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue, 13 Nov 2007 20:05:16 +0000 (GMT)
Subject: [Rd] How to overload the assignment operator?
In-Reply-To: <20071113155703.57610@gmx.net>
References: <20071113155703.57610@gmx.net>
Message-ID: <Pine.LNX.4.64.0711131959350.14849@gannet.stats.ox.ac.uk>

On Tue, 13 Nov 2007, "Jens Oehlschl?gel" wrote:

> Thank you Brian,
>
>> setReplaceMethod() is just syntactic sugar for setting an S4 method on a
>> replacement function (read the function definition to see so).  You can
>> set S3 replacement methods, and the S4 mechanism just piggy-backs on that.
>
> So I understand that setReplaceMethod will not help.
>
>> I think the conceptual problem is that the assignment operator does not
>> actually do any copying: it creates a binding of a symbol to a value.
>> Any copying which occurs happens when the value is (potentially) changed.
>
> I would be even happier if the cloning would only occur on any attempt to change the external pointer / proxy-object.
>
>> There is no provision for that to depend on the class (rather than the
>> type) of the object.
>
> Mh, unless the internal copying mechanism would call a clone generic for non-atomic objects

But as I said, there is no provision for that.  Nor is there going to be. 
One reason is performance, as Simon has hinted.  Another is consistency: 
duplication might well be called from places where class is being ignored.

>> Since external pointers are never duplicated, you
>> ought to be able to take advantage of that to design the copying semantics
>> you want.
>
> How that? How do I know that any user has assigned/modified the external pointer (or a proxy object containing the external pointer) such that I can invoke the cloning?

I don't even know what you want to do.  But I don't think copying is the 
place to find out if a user changed an object: presumably it was done via 
your interface.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


[Rd] How to overload the assignment operator?
Prof Brian Ripley