Chad Goymer
2011-Apr-30 16:35 UTC
[Rd] Reference Classes: Accessing methods via [[...]], bug?
I've been trying to use methods for reference classes via the notation "[[...]]" (X[["doSomething"]] rather than X$doSomething), but it failed to work. However, I did find that if you use the usual "$" notation first, "[[...]]" can be used afterwards. The following simple example illustrates the point:> setRefClass("Number", + fields = list(+ value = "numeric"+ ),+ methods = list(+ addOne = function() {+ value <<- value + 1+ }+ )+ )> X <- new("Number", value = 1)> X[["value"]][1] 1 > X[["addOne"]]()Error: attempt to apply non-function> class(X[["addOne"]]) # NULL[1] "NULL" > class(X$addOne)[1] "refMethodDef"attr(,"package")[1] "methods" > X[["addOne"]]()> X[["value"]][1] 2> class(X[["addOne"]])[1] "refMethodDef"attr(,"package")[1] "methods"Is this a bug? Chad Goymer [[alternative HTML version deleted]]
Hadley Wickham
2011-May-01 02:54 UTC
[Rd] Reference Classes: Accessing methods via [[...]], bug?
If this message is garbled for anyone else, the original question on stackoverflow is here: http://stackoverflow.com/questions/5841339/using-notation-for-reference-class-methods Hadley On Sat, Apr 30, 2011 at 11:35 AM, Chad Goymer <chad.goymer at hotmail.co.uk> wrote:> > I've been trying to use methods for reference classes via the notation "[[...]]" (X[["doSomething"]] rather than X$doSomething), but it failed to work. However, I did find that if you use the usual "$" notation first, "[[...]]" can be used afterwards. The following simple example illustrates the point: >> setRefClass("Number", + ? fields = list(+ ? ? value = "numeric"+ ? ),+ ? methods = list(+ ? ? addOne = function() {+ ? ? ? value <<- value + 1+ ? ? }+ ? )+ )> X <- new("Number", value = 1)> X[["value"]][1] 1 >> X[["addOne"]]()Error: attempt to apply non-function> class(X[["addOne"]]) # NULL[1] "NULL" >> class(X$addOne)[1] "refMethodDef"attr(,"package")[1] "methods" >> X[["addOne"]]()> X[["value"]][1] 2> class(X[["addOne"]])[1] "refMethodDef"attr(,"package")[1] "methods" > Is this a bug? > Chad Goymer > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/
Possibly Parallel Threads
- Internally accessing ref class methods with .self$x is different from .self[['x']]
- Reference Class error message: may be caused by lazy evaluation?
- Class generator functions for reference classes
- reference classes: question on inheritance
- Reference class finalize() fails with 'attempt to apply non-function'