2

I get the folowing Error and I think it is because the interpreter thinks that I call the function .self$getFields.

a<-A$new(var1=list(B$new(var1="a"),B$new(var1="b")))
a$test()
[1] Error in getFields(l, c("var1")) : unused argument (c("var1"))



  AB.getFields<-function(keys){
  vars<-mget(names(.refClassDef@fieldClasses), envir = attr(.self, ".xData"))
  return(vars[keys])
}

B<-setRefClass(Class = "B"
              ,fields = list(var1 = "character")
              ,methods = list(getFields=AB.getFields
                               ,initialize=function(...) {
                                   usingMethods("getFields")
                                   callSuper(...)
                                 }
                               )
)

A<-setRefClass(Class = "A"
              ,fields = list(var1 = "list")
              ,methods = list(getFields=AB.getFields,
                              test=function() {
                                  getFields(.self$var1,c("var1"))
                                 }))

setGeneric("getFields", function(object, ...) standardGeneric("getFields"))
setMethod(getFields, "list", function(object, ...) lapply(object,function(e,...) e$getFields(...)))
Klaus
  • 1,946
  • 3
  • 19
  • 34
  • But, take a look at `?ReferenceClasses`, particularly the 'Debugging' section for a full explanation. – nograpes Sep 17 '13 at 14:18
  • @nograpes, it has not realy something to do with debugging, this effect is besides. The main Problem is that `getField` is not known in the certain function of the object, like I described above. – Klaus Sep 17 '13 at 15:24
  • Well, then I completely don't understand anything you wrote in the question. I don't understand what you mean by "Browser". Do you mean console? If so, how are you running the script outside the console? Is it a script? What do you mean by "trace a function of a certain class"? I guess you don't mean you are using the `trace` function. – nograpes Sep 17 '13 at 15:53
  • @nograpes, I edit my post, is it now clearer? – Klaus Sep 20 '13 at 20:04

1 Answers1

1

Ok, the problem, as you suspected, is a collision name between the name of the Ref class method and the name of the generic function.

For example this fixes the problem (in the console, probably not in a package):

A<-setRefClass(Class = "A"
               ,fields = list(var1 = "list")
               ,methods = list(getFields=AB.getFields,
                               test=function() {
                                 fx <- get('getFields', 1)
                                 fx(.self$var1,c("var1"))
                               }
              )
)

In my opinion, the best way would be to name differently your ref class method. e.g. get_fields.

Karl Forner
  • 4,175
  • 25
  • 32
  • Sry, I edit my post yesterday because some readers dont understand it. By the edit yesterday I do a mistake in setMethod, now I fixed it. So the problem is still the same. – Klaus Sep 18 '13 at 10:50
  • very difficult to help you without an example. Your code works fine with me. – Karl Forner Sep 18 '13 at 13:00
  • Now I could reproduce the Errror. I edit my start post and see what happens. I think the problem of the call `getFields` in the function `A$test()` is, that the interpreter wants to call `.self$getFields`. Do you have a advise to make this executable without renaming `getFields`? – Klaus Sep 19 '13 at 08:20