From jgentry at jimmy.harvard.edu  Thu Apr 21 20:34:00 2005
From: jgentry at jimmy.harvard.edu (Jeff Gentry)
Date: Thu Apr 21 20:31:32 2005
Subject: [Rd] Overloading methods in R
In-Reply-To: <BAY17-F340FDF8018D96F0C3A37FDD12C0@phx.gbl>
Message-ID: <Pine.SOL.4.20.0504211430410.24738-100000@santiam.dfci.harvard.edu>


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.


[Rd] Overloading methods in R
Jeff Gentry