[R] S3 generics need identical signature?

Thomas Lumley tlumley at u.washington.edu
Mon Jun 21 18:38:28 CEST 2010


On Mon, 21 Jun 2010, Henrik Bengtsson wrote:

> On Mon, Jun 21, 2010 at 4:23 PM, Gábor Csárdi <csardi at rmki.kfki.hu> wrote:
>> On Mon, Jun 21, 2010 at 4:17 PM, Duncan Murdoch
>> <murdoch.duncan at gmail.com> wrote:
>> [...]
>>> The requirement is that the methods need to have signatures that contain all
>>> the arguments of the generic.  If the generic includes ..., then the methods
>>> can add other arguments, too.  So with the generic for plot() as you show
>>> above, any plot method is required to have x and y as the first two
>>> arguments, and ... as an argument, but they can have other args too.
>>
>> This makes sense, and it is actually great! Thanks a lot for the explanation.
>
> FYI, the most "generic" way you can write a generic function is:
>
> foo <- function(...) UseMethod("foo");
>

This is only the "most generic" way if you want dispatch on the first argument.  You can have dispatch on a different argument, and that requires you to specify arguments to the generic function.

For example, I have written quite a few functions of the form
    foo <- function(formula, data, ...) UseMethod("foo", data)


         -thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle


More information about the R-help mailing list