R6
R6 copied to clipboard
locked objects + derived classes
The following snippet of code fails:
library(R6)
Base <- R6Class(
"Base",
public = list(
initialize = function() {
self[["Hello"]] <- 1
}
),
lock_objects = FALSE
)
Base$new() # okay
Derived <- R6Class("Derived", inherit = Base)
Derived$new() # barf
The error:
> Derived$new()
Error in self[["Hello"]] <- 1 :
cannot add bindings to a locked environment
Should derived classes also inherit the lockedness from their parent environment?
The behavior you're proposing does seem reasonable... but then what do you suggest as the default value for lock_objects
?
I would lean towards lock_objects = FALSE
, unless modifying that makes it too easy for users to do things that could break R6 in weird ways. (I think modifying properties on self
is probably fine in most cases?)
I think it's too late to change the default behavior to not lock objects -- and I think that it's good that they're locked by default.
What I meant was, it seems reasonable to allow inheriting the lock status, but if we were to change to that behavior, the current default of lock_objects = TRUE
doesn't make sense. So the question is what could it be changed to that makes top-level classes default to being locked, but also makes subclasses inherit the lock status by default?