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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to