Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It is possible to overwrite methods in R6 objects #19

Closed
richfitz opened this issue Aug 11, 2014 · 6 comments
Closed

It is possible to overwrite methods in R6 objects #19

richfitz opened this issue Aug 11, 2014 · 6 comments

Comments

@richfitz
Copy link

Once an R6 object has been constructed, it is possible to overwrite its methods, which seems undesirable:

Thing <- R6::R6Class("Thing",
                     public=list(
                       a=1L,
                       f=function() a))
obj <- Thing$new()
obj$f() # 1L
obj$f <- NULL # is allowed
obj$f() # Error: attempt to apply non-function

And of course, putting the method "back" doesn't work because the environment of that function is wrong:

obj$f <- function() a
obj$f() # Error: object 'a' not found

Is there any way of easily preventing this happen without sacrificing the speed goals of R6?

@richfitz
Copy link
Author

Sorry - should have said: this affects the current CRAN version (1.0.1), as well as development version a1177fc

@wch
Copy link
Member

wch commented Aug 11, 2014

Yes, it probably makes sense to lock the bindings for methods when lock=TRUE. As it is, that option stops you from adding new variables, but doesn't prevent modifying existing ones.

@wch wch closed this as completed in 87d8fb2 Aug 11, 2014
@wch
Copy link
Member

wch commented Aug 11, 2014

I decided that method bindings should always be locked; if users really want to modify them, they can always unlock the bindings themselves.

@mmuurr
Copy link

mmuurr commented Dec 28, 2020

I realized this was closed 6(!) years ago ... but I'm wondering how one would "unlock the bindings themselves". I suddenly find myself in a position where I'd like to update a method after an object has been created (i.e. an update on the instance). I tried creating the generator with lock_objects = FALSE, but I'm still getting the cannot change value of locked binding for 'foo' error. Is this doable with current R6 implementation?

@gaborcsardi
Copy link
Member

unlockBinding() unlocks a binding.

@mmuurr
Copy link

mmuurr commented Dec 28, 2020

@gaborcsardi ahhhh... I hadn't thought of that base function. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants