简体   繁体   中英

How to call standardGeneric with a variable function name

The following code throws a warning:

test_slot = "testslot"

setGeneric(name = test_slot, 
           def = function(object){
               standardGeneric(test_slot)
           },
           where = globalenv()
)



[1] "testslot"
Warning message:
In .recursiveCallTest(body, fname) :
  the body of the generic function for ‘testslot’ calls 'standardGeneric' to dispatch on a different name ("test_slot")!

I would have expected that it would have been equivalent to:

setGeneric(name = "testslot", 
           def = function(object){
               standardGeneric("testslot")
           },
           where = globalenv()
)

What happened?

This does not answer why the original block of code failed, but a workaround would be to do:

test_slot = "testslot"

setGeneric(name = test_slot, 
           def = eval(parse(text = paste0("function(object){",

           "standardGeneric('", test_slot, "')",
       "}")))
           },
           where = globalenv()
)

that is, construct the entire def argument to be an evaluated function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM