[Rd] default arguments in generics and methods

Parlamis Franklin fparlamis at mac.com
Thu Oct 19 20:52:42 CEST 2006


i believe the following is true but would appreciate confirmation  
that it is intended behavior and will continue:

if a default argument is employed in the definition of a generic  
function, and the generic is called with the argument in question  
(call it 'ARG') missing, then the method for signature (..., ARG =  
"missing", ...) will be called by 'standardGeneric' whether or not  
ARG is evaluated in the body of the generic prior to the call to  
'standardGeneric';  however, the value assigned to ARG in the method  
call will depend on whether ARG was evaluated in the body of the  
generic prior to the call to 'standardGeneric' (with the default  
value assigned in the case of prior evaluation and no value assigned  
in the case of no prior evaluation).  furthermore, if ARG is  
explicitly assigned to in the body of the generic prior to the call  
to 'standardGeneric', then the method for signature (..., ARG = class 
(ARG), ...) will be called, with the assigned value of ARG passed to  
the call.

this behavior is exhibited by the code below:

__

setGeneric("foo", function(x = 1) standardGeneric("foo"),  
useAsDefault = FALSE)
setMethod("foo", signature(x = "missing"), function(x = 3) x)

setGeneric("foo2", function(x = 1) {x; standardGeneric("foo2")},  
useAsDefault = FALSE)
setMethod("foo2", signature(x = "missing"), function(x = 3) x)

setGeneric("foo3", function(x = 1) {x<-2; standardGeneric("foo3")},  
useAsDefault = FALSE)
setMethod("foo3", signature(x = "missing"), function(x = 3) x)
setMethod("foo3", signature(x = "numeric"), function(x = 3) x^2)

foo()		# returns 3
foo2()		# returns 1
foo3()		# returns 4

___

franklin parlamis




More information about the R-devel mailing list