If an environment x contains a locked binding y which is also an environment, and then you try to assign a value to a binding inside of y, it can either succeed or fail, depending on how you refer to environment y. x <- new.env() x$y <- new.env() lockEnvironment(x, bindings = TRUE) # This assignment fails x$y$z <- 1 # Error in x$y$z <- 1 : cannot change value of locked binding for 'y' # Saving x$y to another variable, and then assigning there works y2 <- x$y y2$z <- 10 # OK print(x$y$z) # 10 Is this a bug or a feature? I realize that x$y is a locked binding while y2 is not. -Winston
Error when assigning value in environment which is a locked binding
3 messages · Winston Chang, Simon Urbanek
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20140810/f6ae566a/attachment.pl>
On Aug 10, 2014, at 10:07 PM, Winston Chang <winstonchang1 at gmail.com> wrote:
Another oddity - even though there's an error thrown in assignment to x$y$z, the assignment succeeds. x <- new.env() x$y <- new.env() lockEnvironment(x, bindings = TRUE) x$y$z <- 1 # Error in x$y$z <- 1 : cannot change value of locked binding for 'y' x$y$z # [1] 1 So I assume there must be a bug somewhere in here.
Why? In both cases (including your previous post) the you have two separate assignments: (x$y)$z <-1 followed by x$y <- y. So the first one always succeeds since there is no reason for it not to, but the second one doesn't because the binding is locked. I presume the confusion comes from the fact that environments are mutable, so x$y changes even if it was not assigned so it's irrelevant if the second assignment succeeds or not. Cheers, Simon
-Winston On Sun, Aug 10, 2014 at 8:46 PM, Winston Chang <winstonchang1 at gmail.com> wrote:
If an environment x contains a locked binding y which is also an environment, and then you try to assign a value to a binding inside of y, it can either succeed or fail, depending on how you refer to environment y. x <- new.env() x$y <- new.env() lockEnvironment(x, bindings = TRUE) # This assignment fails x$y$z <- 1 # Error in x$y$z <- 1 : cannot change value of locked binding for 'y' # Saving x$y to another variable, and then assigning there works y2 <- x$y y2$z <- 10 # OK print(x$y$z) # 10 Is this a bug or a feature? I realize that x$y is a locked binding while y2 is not. -Winston
[[alternative HTML version deleted]]
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel