I've found that reference class objects tend to behave like plain old
environments wrt field access, unless a method on e.g. $<- is explicitly
defined.

Here is a code snippet:

library(methods)

Foo <- setRefClass("Foo")
foo <- Foo$new()
foo$a <- 2 # why does this succeed? not a valid field!
## set a silly $<- method
setReplaceMethod("$", "Foo", function(x, name, value) {
  callNextMethod(x, "b", value)})

foo$b <- 3


At first, we can set the non-existent field "a" without any error (I don't
think this is the intended behavior). Then we set a silly override on $<-
and things start to behave as expected:

Error in envRefSetField(x, what, refObjectClass(x), selfEnv, value) :
  "b" is not a field in class "Foo"

What am I doing wrong?

Thanks,
Michael

        [[alternative HTML version deleted]]

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to