Closed pchabros closed 1 year ago
The option lock_objects=FALSE
makes it possible to add new members to an object, but it doesn't make it possible to alter "normal" members that are added at construction time (ones which are listed in public
, private
, or active
).
At a low level, lock_objects=FALSE
works by not locking the environment that the constitutes the R6 object. However, the members that were added at construction time are still locked bindings in the environment.
If you want to change a locked binding, you can use unlockBinding()
, as in:
Parent <- R6::R6Class(
"Parent",
lock_objects = FALSE,
public = list(
initialize = function() {
unlockBinding("foo", self)
# I wan't to create many methods dynamically in the loop.
self[["foo"]] <- function() {
print('foo')
}
}
)
)
Child <- R6::R6Class(
"Child",
inherit = Parent,
lock_objects = FALSE,
public = list(
foo = function() {
super$foo()
}
)
)
child <- Child$new()
However, if you do this, some things may not work as expected, like cloning.
Reproducible example:
Error:
Why I'm getting this error when in both Parent and Child
lock_objects = FALSE
?