From jmc at r-project.org  Fri Mar 29 19:30:36 2013
From: jmc at r-project.org (John Chambers)
Date: Fri, 29 Mar 2013 11:30:36 -0700
Subject: [Rd] Redefining quote() with reference classes (was: Ref
	classes initFields has incorrect environment?)
In-Reply-To: <CABdHhvFOcTbX0YnQ8Uiz6C_QdWX_hu9kuA7gD8XgDLg+voEzrQ@mail.gmail.com>
References: <CABdHhvFOcTbX0YnQ8Uiz6C_QdWX_hu9kuA7gD8XgDLg+voEzrQ@mail.gmail.com>
Message-ID: <3FB0FB82-F3E3-4911-93B9-D2729246AC94@r-project.org>

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



[Rd] Redefining quote() with reference classes (was: Ref classes initFields has incorrect environment?)
John Chambers