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

bgunter@gene.com bgunter at gene.com
Wed Jul 20 17:25:22 CEST 2005


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


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.
******
However:

> 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

Cheers,
Bert Gunter



More information about the R-devel mailing list