r-lib / R6

Encapsulated object-oriented programming for R
https://R6.r-lib.org
Other
407 stars 56 forks source link

Suggestion: more informative error message when trying to assign values to active fields that do not support this #39

Closed jankowtf closed 9 years ago

jankowtf commented 9 years ago

Just a suggestion: it might be worth considering if the error message could be a bit more informative when trying to assign a value to active fields whose binding functions don't take any arguments.

Consider the following example:

MyClass <- R6Class(
  classname = "MyClass",
  public = list(
    x = "character",
    initialize = function(
      x = "a"
    ) {
      self$x <- x
    }
  ),
  active = list(
    y = function() file.path(self$x, "b")
  )
)
inst <- MyClass$new()
inst$x
inst$y

The binding function of y does not take any arguments as it's supposed to be strictly dependent on x,

inst$y <- "a/b/c"
# Error in (function ()  : unused argument (quote("a/b/c"))

Here, something like this might be better:

You tried to assign a value to an active field that does not support this (strict dependency)

AFAIK, this would need to be supplied somewhere in the call to makeActiveBinding(). Probalby something along the lines of this:

makeActiveBinding(
  "test", 
  env = .GlobalEnv,
  function(v) {
    if (missing(v)) {
      print("Execute the binding function")
    } else {
      stop("You tried to assign a value to an active field that does not support this (strict dependency)")
    }
  }
)
> test
[1] "Execute the binding function"
> test <- "a"
Error in (function (v)  : 
  You tried to assign a value to an active field that does not support this (strict dependency)
wch commented 9 years ago

I agree that the error is confusing, but I any reasonable solution adds too much complexity, which goes against the design goals of R6.

jankowtf commented 9 years ago

Jup, makes sense from that perspective