From jmc at r-project.org  Wed Mar 11 17:57:43 2009
From: jmc at r-project.org (John Chambers)
Date: Wed, 11 Mar 2009 09:57:43 -0700
Subject: [Rd] Could you please add "time<-" as a generic function in the
 'stats' package ?
In-Reply-To: <20090311145557.5c9b57e0@mimi>
References: <20090311145557.5c9b57e0@mimi>
Message-ID: <49B7ED87.3090102@r-project.org>

Whatever one wants for an S3 generic, it's not needed to do what, 
presumably, you want here.

And for sure it is no excuse for S3 methods for S4 classes.

Back to basics: To write S4 methods for an existing function, the clean 
and simple way is usually:

setGeneric("time<-")

If your package depends on one that has S3  methods for this function, 
there will be a version of the function imported into your namespace.  
That function will then be the default method.

Presumably you want to ensure that S3 methods, for S3 classes, are still 
dispatched.  Quite reasonable and it should follow from the call to 
setGeneric.

If you wanted to have your own S3 methods or if you weren't willing to 
assume an S3 generic imported, you could do a 2-line version:

R(r48103)> `time<-` <- function(x, value) UseMethod("time<-")
R(r48103)> setGeneric("time<-")
[1] "time<-"
R(r48103)> showMethods("time<-", include = TRUE)
Function: time<- (package .GlobalEnv)
x="ANY"
function (x, value)
UseMethod("time<-")

As a postscript, here is the current plan, not yet committed, pending 
some more testing:
  - the bad methods will be allowed
  - warnings when a class is defined with such methods for a superclass
  - probably some other warnings, but not for an ordinary call to the 
method (it's the MISSING calls to the method that are the disaster).

More later,
  John


Yohan Chalabi wrote:
> Dear R developers,
>
> As you might have noticed, recent changes in R-dev will not allow the  
> definition of S3 methods with S4 classes.
>
> But until now, we have defined "time<-" in our 'timeSeries' package as  
> an S3 generic because other packages are using the same function.  
> Indeed, if we had defined it as an S4 generic, the other packages  
> would not coexist well with ours.
>
> Another package might overwrite the generic and its methods when both  
> packages are loaded.
>
> In my understanding the only way to avoid this problem is to add
>
> `time<-`
> function (x, value)
> {
>     UseMethod("time<-")
> }
>
> in the 'stats' package.
>
> As a wish for the forthcoming R version I would like to ask you if you  
> could add "time<-" as a generic function in the 'stats' package to  
> prevent conflicts and to ensure that packages continue to work well  
> together.
>
> Thank you in advance for your feedback!
>
>
>



[Rd] Could you please add "time<-" as a generic function in the 'stats' package ?
John Chambers