R's UseMethod() does not dispatch on changed class() (PR#167)

maechler@stat.math.ethz.ch maechler@stat.math.ethz.ch
Wed, 14 Apr 1999 11:23:20 +0200


       (opening new bug report thread, on suggestion of BDR ..)

I said on vaguely related topic:

 MM> There's  another long-standing  
 MM> UseMethod / NextMethod / Dispatch problem that I've never traced/solved..
 MM>  
 MM> Look at the  $RHOME/tests/mode-methods.R   file !

Peter Dalgaard already remarked :

 PD> ..i.e. this:

 PD> abc <- function(x, ...) {
 PD>     if (is.null(class(x))) class(x) <- data.class(x)
 PD>     UseMethod("abc")
 PD> }
 PD> 
 PD> will dispatch on the data class in S but not in R.
 PD> 
 PD> This puzzles me, because normally you can't mess with the arguments of
 PD> a generic function, in *either* language:
 PD> 
 PD> > cde<-function(x,...){x<-x*2;UseMethod("print")}
 PD> > cde(2)
 PD> [1] 2
 PD> 
 PD> So, either it's because class<- is non-duplicating in S, or S is doing
 PD> something *really* strange here. 
 PD> 
 PD> Hmm, then again: class<- *is* non-duplicating in R:
 PD> 
 PD> > x<-1
 PD> > y<-.Alias(x)
 PD> > class(x)<-'foo'
 PD> > y
 PD> [1] 1
 PD> attr(,"class")
 PD> [1] "foo"
 PD> 
 PD> I think this is related to evaluation of promises:
 PD> 
 PD> > f<-function(x)substitute(x)
 PD> > f(sin(1))
 PD> sin(1)
 PD> > f<-function(x){x;substitute(x)}
 PD> > f(sin(1))
 PD> sin(1)
 PD> > f<-function(x){class(x)<-'foo';substitute(x)}
 PD> > f(sin(1))
 PD> [1] 0.841471
 PD> attr(,"class")
 PD> [1] "foo"
 PD> 
 PD> Notice that just forcing evaluation of x does not destroy the link
 PD> between x and the argument expression, but changing its class does.
 PD> This is probably what shouldn't happen.
 PD> 
 PD> You can get the dispatching done by using
 PD> 
 PD> abc <- function(x, ...) {
 PD>     if (is.null(class(x))) {
 PD> 	class(x) <- data.class(x)
 PD> 	abc(x, ...)
 PD>     } else
 PD>         UseMethod("abc")
 PD> }
 PD> 
 PD> which (seemingly) works in both languages, at least as long as you
 PD> don't have to mess with sys.parent()

So we don't know if this is a bug in R.
However, R is behaving inexpectedly to ``Joe Average-User''
and at least we should know if (and why)
we want to be different than S here.

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._