You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
library(R6)
FooClass= R6Class(
"Foo",
public=list(
initialize=function(value) {
# force(value) # does not helpself$foo.fun=function(x) x==value
},
foo.fun=NULL
)
)
foo.obj1=FooClass$new(3)
foo.obj1$foo.fun(3)
# TRUEfoo.obj2=foo.obj1$clone()
foo.obj2$foo.fun(3)
# value not foundfoo.obj3=foo.obj1$clone(deep=TRUE)
foo.obj3$foo.fun(3)
# value not found
Or is this expected to fail?
The text was updated successfully, but these errors were encountered:
That is the expected behavior - the clone() method assumes that any function in the object (like foo.fun 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 foo.fun 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.
Consider the following example
Or is this expected to fail?
The text was updated successfully, but these errors were encountered: