Let the following code:
x <- 1:5
class(x) <- "bar"
foo <- function(x) {
UseMethod("foo")
}
foo.bar <- function(x) {
y <- max(x)
return(y)
}
I would like the output of foo(x)
to have the same class as x
(i.e., "bar"). The obvious way to do it is to add a class(y) <- class(x)
inside foo.bar()
, but I would like to know if/how I could do that in foo()
itself.
The reason for this is that my real case has several generics, each one with 10+ methods, so if I could inherit the class in the generics, I'd just have to modify those instead of tens of methods.