如何为同一类的对象列表定义方法?
例如。
foo <- letters[1:5]
foo2 <- letters[6:10]
class(foo) <- "abc"
class(foo2) <- "abc"
new_method <- function(...) {UseMethod("new_method", ...)}
new_method.abc <- function(...) {do.call("c", list(...))}
# use results in error
new_method(foo,foo2)
Error in new_method(foo, foo2) : '...' used in an incorrect context
在这里,我想...
成为一个任意长度的对象列表,所有这些对象都具有相同的类,并且我想对它们做一些事情(将它们组合起来,特定于我的真实类的用例)。
...
对我来说,没有可以发送到方法调度的类是有道理的;但是一个简单的重写也不起作用,因为new_method.list
不/不应该存在
new_method <- function(...) {UseMethod("new_method", list(...))}
new_method(foo,foo2)
Error in UseMethod("new_method", list(...)) :
no applicable method for 'new_method' applied to an object of class "list"