-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cloning of classes with objects that reference each other #179
Comments
I like the idea of a One advantage that This would also allow users to make I think that when it comes to replacing the built-in clone method entirely with a custom version (the workaround suggested in #110), it is basically impossible to expect a class authors to get it right in the general case, so it would be good to allow them to call R6's built-in cloning code and then massage the result afterward. (Getting cloning right for simple classes is not too hard, but when inheritance and active bindings come into play, it becomes very, very hard. The are currently almost 1000 lines of code for the cloning tests.) |
I am mostly using R6 to wrap C++ classes. In my use case, I have a Also, not modifying the I like the |
I also really like the idea of a either a Currently I have set Has any more thought been given to implementing one of these methods in a future release of this package? |
A lot of the examples of cloning challenges with nested R6 objects have used the relatively simple Have such methods been discussed (or are in the works) for R6 classes? |
- Rename clone() to .clone(). - Create a default clone() that calls .clone() - Add test for overriding clone() See r-lib#179 (comment) for details
If a class defines a post_clone() method, it will automatically be called on the newly created object. This is convenient because post_clone() allows to modify private fields of the new object at its creation. See r-lib#179 (comment)
I already commented on this in #110, but I'd like to get an "official" opinion on this (and I could make a PR if this is something desirable). Currently it is very difficult to
clone(deep = TRUE)
an object that has member objects that reference each other.An example would be the following:
r2
contains objecty
, which references objectx
. Changingx
through the reference iny
is possible in the original objectobj1
, but not the cloneobj2
:A workaround was posted by @dfalbel, but there are two drawbacks to this: (1) overwriting the
clone
method completely means having to construct the objects from scratch, and (2) the method is less than well-supported and actually seems to be working around the restriction built intoR6::R6Class
which seems to try to prevent customclone
methods.It would be useful to have the option to let some code execute after most of the
clone()
work was done, to do some post-processing on the cloned object. In the example above it would then be possible to repair the reference in they$ref
object, for example. An API that I suggest is to have give the user the option to define aprivate$post_clone(old_self)
method, that gets called at the end ofclone()
. Changes required would probably be inserting the linesat the very end of he clone function.
I would be interested in hearing your opinion on this.
The text was updated successfully, but these errors were encountered: