Hadley Wickham
2013-Mar-28 18:40 UTC
[Rd] Ref classes initFields has incorrect environment?
As suggested by the following code: A <- setRefClass("A", fields = list(x = "logical"), methods = list(initialize = function(...) { x <<- FALSE initFields(...) })) A$new() # Works as expected quote <- as.character A$new() # Error in function (value) : # invalid replacement for field ?x?, should be from class ?logical? or a subclass (was class ?character?) I get the same error in: * R version 2.15.2 (2012-10-26) * R version 2.15.3 (2013-03-01) * R Under development (unstable) (2013-03-28 r62432) Hadley -- Chief Scientist, RStudio http://had.co.nz/
John Chambers
2013-Mar-29 18:30 UTC
[Rd] Redefining quote() with reference classes (was: Ref classes initFields has incorrect environment?)
Nothing to do with initFields. If you trace your redefined quote(), it's called from the <<- assignment of x. The "x" element in the environment for the reference class object is implemented as an active binding in order to enforce the class when assigning the field. Effectively that makes the assignment behave like a reference class method, and so ends up doing as() and getting back to the malware version of quote(). The fix will be to have a more bullet proof (and perhaps more efficient) version of the active binding generated for fields. A better design anyway, but this will take a little re-organization because the current default binding function is generated by some code manipulation. Meanwhile the workaround is: Don't do what the revised subject heading says. John On Mar 28, 2013, at 11:40 AM, Hadley Wickham <h.wickham at gmail.com> wrote:> As suggested by the following code: > > A <- setRefClass("A", fields = list(x = "logical"), > methods = list(initialize = function(...) { > x <<- FALSE > initFields(...) > })) > A$new() > # Works as expected > > quote <- as.character > A$new() > # Error in function (value) : > # invalid replacement for field ?x?, should be from class ?logical? > or a subclass (was class ?character?) > > I get the same error in: > * R version 2.15.2 (2012-10-26) > * R version 2.15.3 (2013-03-01) > * R Under development (unstable) (2013-03-28 r62432) > > Hadley > > -- > Chief Scientist, RStudio > http://had.co.nz/ > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Apparently Analagous Threads
- initFields() method no longer coerces arguments in R-devel
- General "nil" reference class object
- Dabbling with R5 setRefClass - Inheritance problems
- Reference Classes: Generalizing Reference Class Generator objects?
- possibly invalid assertion in setRefClass?