[Rd] Overloading methods in R

Jeff Gentry jgentry at jimmy.harvard.edu
Thu Apr 21 20:34:00 CEST 2005


On Thu, 21 Apr 2005, Ali - wrote:
> >However, in S3 you can create a "generic" generic function by not 
> >specifying
> >arguments but only '...' - this way any methods can take any arguments (and
> >you don't force your argument names onto other developer's).
> So why did they go a step backward in S4 and remov this feature?

I might be misunderstanding what you're getting at here, but if indeed I
do understand this correctly then not only is it still possible in S4 but
I was tought that it was generally considered Good Behavior.

Consider the following code snippet:

> setClass("foo", representation(aStr="character"))
[1] "foo"
> setClass("bar", representation(aStr="character"))
[1] "bar"
> setGeneric("testFun", function(object, ...)
+ standardGeneric("testFun"))
[1] "testFun"
> setMethod("testFun", "foo", function(object, x)
+ print(x))
[1] "testFun"
> setMethod("testFun", "bar", function(object, a, b, c)
+ print(a+b+c))
[1] "testFun"
> z <- new("foo", aStr="")
> x <- new("bar", aStr="")
> testFun(z, "blah")
[1] "blah"
> testFun(x, 1, 2, 3)
[1] 6

Here I've defined a generic 'testFun' and assigned it the '...' argument
(note that you could still specify required arguments,
e.g. 'function(object, anArg, anotherArg, ...)' in the segGeneric
command).  Then when I use setMethod(s) for the two classes I am able to
specify differing sets of arguments for each class.



More information about the R-devel mailing list