From buser at stat.math.ethz.ch  Thu Jan 31 13:54:32 2008
From: buser at stat.math.ethz.ch (Christoph Buser)
Date: Thu, 31 Jan 2008 13:54:32 +0100
Subject: [Rd] S4: setGeneric() and setMethod() with changed argument order
Message-ID: <18337.50440.781965.267262@stat.math.ethz.ch>

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/



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