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

John Chambers jmc at R-project.org
Wed Jul 20 23:41:18 CEST 2005


Quite right.  There is code in the evaluator that copies default values 
from the method into the promise for a missing argument, when the method 
is being evalutated.

The catch seems to be that there is no promise unless SOME default was 
included for the argument in the generic.

So either we need to adjust the generic accordingly, perhaps when the 
setMethod() with a default is done (this could be tricky with multiple 
packages having methods for the same generic), or else admit the 
ugliness in the documentation.

Thanks.

bgunter at gene.com wrote:

> 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
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



More information about the R-devel mailing list