Re: [Rd] Reference Class error message: may be caused by lazy evaluation?

From: John Chambers <jmc_at_r-project.org>
Date: Thu, 09 Jun 2011 10:11:21 -0700

Good catch.

Here's the problem. To save space and time, reference methods are not all copied to every object in the class. Instead, the methods are copied in when first accessed. Methods are functions which use the object as their environment. So that is the sense in which "lazy evaluation" is involved.

If a method calls another method (add() calling addOne() in your example), then the method for the `$` operator knows to copy over that method (addOne). (The second of my examples below shows this.) But if the method _refers_ to another method without calling it, the code analysis does not currently catch the reference.

We can fix that, although it's a little subtle. Meanwhile, your showself() is a good workaround.

For anyone interested, the code below illustrates.

One point of style. I would suggest saving the generator object and calling its $new() method, as below, rather than treating the reference class as an S4 class. The result is identical AFAIK, but the style is more typical of such OOP languages.

John



> tc <- setRefClass("testclass", fields = list(a = "numeric"),
+ methods = list(
....
+ ))
> t1 <- tc$new(a=1)
> ss = t1$show
> ss

Class method definition for method show() function ()
{

     print(addOne)
}
<environment: 0x10188d5f8>
> ev = environment(ss)
> ev

<environment: 0x10188d5f8>
> t1

An object of class "testclass"
<environment: 0x10188d5f8> # <<<< same environment
> objects(ev)

[1] "a" "show" #<<<< not addOne, though
> t1$addOne

Class method definition for method addOne() function ()
{

     a <<- a + 1
     print(a)

}
<environment: 0x10188d5f8>
> objects(ev)

[1] "a" "addOne" "show" # <<<< now addOne is there

On 6/8/11 4:38 PM, Tengfei Yin wrote:

> Dear All,
>
> I came across an error message recently when constructing a reference class,
> an example is attached below, it looks like only if I call a specific method
> in advance, otherwise it cannot be found in defined method without using
> .self, this make it difficulty that sometimes in my initialize method, I
> need to call other method defined in the same reference class, the
> workaround for this is add .sef to it.
>
>
> ###############  example begin ############################
> setRefClass("testclass", fields = list(a = "numeric"),
>              methods = list(
>                addOne = function(){
>                  a<<- a+1
>                  print(a)
>                },
>                add = function(){
>                  addOne()
>                },
>                show = function(){
>                  print(addOne)
>                },
>                showself = function(){
>                  print(.self$addOne)
>                }
>                ))
>
> obj<- new("testclass", a = 1)
> obj$show()                                                              #
> Error in print(addOne) : object 'addOne' not found
> obj$addOne()                                                           #
> return 2, works
> obj$show()                                                              #
> after calling addOne(), show() works
>
> ## works if use .self$...
> obj2<- new("testclass", a = 1)
> obj2$showself()
>
> ## works when call the method directly within another method
> obj3<- new("testclass", a = 1)
> obj3$add()
>
> ################ end ##########################################
>
> I am still learning this new technique, if I made any mistake I didn't
> notice before, please let me know, I will really appreciate that.
>
> Thanks a lot!
>
> Tengfei
>

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu 09 Jun 2011 - 17:14:06 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 Thu 09 Jun 2011 - 18:00:17 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