[Rd] Default arguments for setMethod() (PR#8021)

From: <bgunter_at_gene.com>
Date: Wed 20 Jul 2005 - 15:25:22 GMT

Full_Name: Bert Gunter
Version: 2.1.1
OS: Windows 2000
Submission from: (NULL) (

There appears to be either a bug or documentation problem in setMethod/setGeneric with how default arguments are handled. The setMethod Help says:

Method definitions can have default expressions for arguments. If those arguments are then missing in the call to the generic function, the default expression in the method is used. If the method definition has no default for the argument, then the expression (if any) supplied in the definition of the generic function itself is used.


> setGeneric('foo',function(x,y)standardGeneric('foo'))
[1] "foo"
> setMethod('foo','numeric',function(x,y=3)x+y)
[1] "foo"
> foo(10)

Error in foo(10) : argument "y" is missing, with no default

#### BUT adding a NULL default argument in the standardGeneric fixes this:

> setGeneric('foo',function(x,y=NULL)standardGeneric('foo'))
[1] "foo"
> setMethod('foo','numeric',function(x,y=3)x+y)
[1] "foo"
> foo(10)

[1] 13

Bert Gunter

R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu Jul 21 01:42:51 2005

This archive was generated by hypermail 2.1.8 : Mon 20 Feb 2006 - 03:21:15 GMT