Skip to content
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

Deep clone does not lead to a complete copy #133

jakob-r opened this issue Oct 16, 2017 · 2 comments


Copy link

commented Oct 16, 2017

Consider the following example


FooClass = R6Class(
  public = list(
    initialize = function(value) {
      # force(value) # does not help
      self$ = function(x) x == value
    }, = NULL

foo.obj1 = FooClass$new(3)

foo.obj2 = foo.obj1$clone()
# value not found

foo.obj3 = foo.obj1$clone(deep = TRUE)
# value not found

Or is this expected to fail?


This comment has been minimized.

Copy link

commented Oct 16, 2017

That is the expected behavior - the clone() method assumes that any function in the object (like in this case) is a method, and when it makes the copy of that function in the clone object, it reassigns the function's environment so that it can find the appropriate self object.

Your version of the captures the environment from the initialize method, but that environment gets lost in the cloning process. In this particular case, you'd be better off storing value in self or private instead of capturing it with a closure, but that may or may not be appropriate for your real use case.

This is essentially the same issue as #94.

I think this the documentation should probably do a better job explaining how cloning works.


This comment has been minimized.

Copy link

commented Oct 17, 2017

Thanks for your comprehensible answer. That solves the issue for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.