我有一个R语言中的通用方法:
setGeneric(
"doWork",
function(x) {
standardGeneric("doWork")
})
setMethod(
"doWork",
signature = c("character"),
definition = function(x) {
x
})
我该如何在定义中添加省略号参数?
setGeneric("doWork", function(x, ...) standardGeneric("doWork"))
setMethod("doWork", signature = c("character"),
function(x, ...) do.call(paste, list(x, ..., collapse=" "))
)
然后:
> doWork("hello", "world", letters[1:5])
[1] "hello world a hello world b hello world c hello world d hello world e"
> doWork(1:3, "world", letters[1:5])
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘doWork’ for signature ‘"integer"’
...
进行调度。来源于?dotsMethods
:setGeneric("doWork2", function(...) standardGeneric("doWork2"))
setMethod("doWork2", signature = c("character"),
definition = function(...) do.call(paste, list(..., collapse=" "))
)
doWork2("a", "b", "c") # [1] "a b c"
doWork2("a", 1, 2) # Error