Closed jakob-r closed 6 years ago
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.
This is essentially the same issue as #94.
I think this the documentation should probably do a better job explaining how cloning works.
Thanks for your comprehensible answer. That solves the issue for me.
Consider the following example
Or is this expected to fail?