[Rd] setting invalid fields on reference classes sometimes allowed

From: Michael Lawrence <lawrence.michael_at_gene.com>
Date: Sun, 05 Aug 2012 09:00:36 -0700


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_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sun 05 Aug 2012 - 16:07:59 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 05 Aug 2012 - 23:40:37 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive