[Rd] S4: setGeneric() and setMethod() with changed argument order

Christoph Buser buser at stat.math.ethz.ch
Thu Jan 31 13:54:32 CET 2008


Dear R devel team

Using S4 classes in R I found a possible source of mistakes
caused by an inattentive user.

If one uses another order of the arguments for a function in the
setGeneric() as in the setMethod() call, one can get undesired
side effects without any warning.

Would it be desirable to include a warning for such cases? Thank
you for a statement.

R code:

## set generic function "test1" with 3 arguments
setGeneric("test1", function(object, printit = TRUE, name = "tmp")
           standardGeneric("test1"))

## Set method for test1 and class "numeric", but change the order od the 2nd
##  and 3rd arguments -> No warning, everything works well?
setMethod("test1",  signature(object = "numeric"),
          function(object, name = "tmp", printit = TRUE) {
              match.call()
          })

## .local is generated with the order from setGeneric(), but is called
## afterwards with the wrong order from setMethod() which can cause an error
## (if you are lucky) or strange results.
selectMethod("test1", signature = signature(object = "numeric"))

> Method Definition:
> 
> function (object, printit = TRUE, name = "tmp") 
> {
>     .local <- function (object, name = "tmp", printit = TRUE) 
>     {
>         match.call()
>     }
>     .local(object, printit, name)
> }
> 
> Signatures:
>         object   
> target  "numeric"
> defined "numeric"
> 

## Shows that for the argument "name" printit is used. For the argument
## "printit" name is used
test1(pi)

> .local(object = object, name = printit, printit = name)



Best regards,

Christoph Buser

--------------------------------------------------------------
Christoph Buser <buser at stat.math.ethz.ch>
Seminar fuer Statistik, LEO C13
ETH Zurich	8092 Zurich	 SWITZERLAND
phone: x-41-44-632-4673		fax: 632-1228
http://stat.ethz.ch/~buser/



More information about the R-devel mailing list