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

Error message when trying to set read-only active bindings #99

Closed
richierocks opened this issue Nov 8, 2016 · 2 comments
Closed

Error message when trying to set read-only active bindings #99

richierocks opened this issue Nov 8, 2016 · 2 comments

Comments

@richierocks
Copy link
Contributor

If I create a class with a read-only active binding, then try to set the value, the error message is a bit obscure. To reproduce:

library(R6)
thing_factory <- R6Class(
  "Thing",
  private = list(
    ..a_field = "a value"
  ),
  active = list(
    a_field = function() {
      private$..a_field
    }
  )
)
a_thing <- thing_factory$new()
a_thing$a_field <- "a new value"
## Error in (function ()  : unused argument (quote("a new value"))

It would be clearer for the user if the message explained that a_field was read-only.

@wch
Copy link
Member

wch commented Nov 8, 2016

Unfortunately, this is what R does with active bindings:

e <- new.env()
makeActiveBinding("x", function() { 1 }, e)
e$x <- 2
# Error in (function ()  : unused argument (quote(2))

I wish there were a straightforward way to make nicer error messages, but I don't think it's possible without changing code in R itself.

@wch wch closed this as completed Nov 8, 2016
@richfitz
Copy link

richfitz commented Nov 8, 2016

What about:

e <- new.env()
makeActiveBinding("x", function(value) {
  if (!missing(value)) {
    stop("x is read only", call. = FALSE)
  }
  1
}, e)
e$x
e$x <- 2

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

3 participants