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

From: Tengfei Yin <yintengfei_at_gmail.com>
Date: Wed, 08 Jun 2011 16:38:52 -0700


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

-- 
Tengfei Yin
MCDB PhD student
1620 Howe Hall, 2274,
Iowa State University
Ames, IA,50011-2274
Homepage: www.tengfei.name

	[[alternative HTML version deleted]]

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