Closed GregorDeCillia closed 7 years ago
This sounds like a duplicate of #108.
It turns out this is actually different from #108. The problem doesn't have to do with active bindings per se, but it happens to come up in classes with active bindings, because of the way that as.list.environment
sorts active bindings separately from other objects.
Here's a minimal example:
library(R6)
A <- R6Class("A",
public = list(
methodA = function() "A"
),
active = list(
x = function() "x"
)
)
B <- R6Class("B",
inherit = A,
public = list(
methodB = function() {
super$methodA()
}
)
)
C <- R6Class("C",
inherit = B,
public = list(
methodC = function() {
super$methodB()
}
)
)
C1 <- C$new()
C2 <- C1$clone()
C2$methodC()
# Error in super$methodB() : object 'super' not found
The problem is in this line of code: https://github.com/wch/R6/blob/41f942af/R/clone.R#L58
It makes the assumption that all of the functions in the super
object have the same enclosing environment. However, this is not true when there are two levels of inheritance. C's super
object may have methods declared in B
as well as some that were inherited from A
. This is shown by:
lapply(C1$.__enclos_env__$super, environment)
# $x
# <environment: 0x10da95228>
#
# $clone
# <environment: 0x10da8fce8>
#
# $methodB
# <environment: 0x10da8fce8>
#
# $A_method
# <environment: 0x10da95228>
Thank you for the quick fix. 073b6f0daba8adc476df53a90d221a7df69a0ea1 solved all my issues in the minimal example as well as my actual project.
I currently have an issue with cloned objects. My setup looks roughtly like this
It seems like
B
can't access the methods of its parentA
via a call tosuper
(in line # 18). The error does not occur, if the active memberns
is ommitedAny help would be appreciated!