From p.dalgaard@biostat.ku.dk  Wed Apr 14 09:21:40 1999
From: p.dalgaard@biostat.ku.dk (Peter Dalgaard BSA)
Date: 14 Apr 1999 10:21:40 +0200
Subject: outer fails with group generic operations on factors (PR#166)
In-Reply-To: Martin Maechler's message of "Wed, 14 Apr 1999 08:31:01 +0200"
References: <199904131729.TAA18570@biostat.ku.dk> <x2g164gzze.fsf@blueberry.kubism.ku.dk> <199904140631.IAA29602@sophie.ethz.ch>
Message-ID: <x27lrfa9yj.fsf@blueberry.kubism.ku.dk>

Martin Maechler <maechler@stat.math.ethz.ch> writes:

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

..i.e. this:
abc <- function(x, ...) {
    if (is.null(class(x))) class(x) <- data.class(x)
    UseMethod("abc")
}

will dispatch on the data class in S but not in R.

This puzzles me, because normally you can't mess with the arguments of
a generic function, in *either* language:

> cde<-function(x,...){x<-x*2;UseMethod("print")}
> cde(2)
[1] 2

So, either it's because class<- is non-duplicating in S, or S is doing
something *really* strange here. 

Hmm, then again: class<- *is* non-duplicating in R:

> x<-1
> y<-.Alias(x)
> class(x)<-'foo'
> y
[1] 1
attr(,"class")
[1] "foo"

I think this is related to evaluation of promises:

> f<-function(x)substitute(x)
> f(sin(1))
sin(1)
> f<-function(x){x;substitute(x)}
> f(sin(1))
sin(1)
> f<-function(x){class(x)<-'foo';substitute(x)}
> f(sin(1))
[1] 0.841471
attr(,"class")
[1] "foo"

Notice that just forcing evaluation of x does not destroy the link
between x and the argument expression, but changing its class does.
This is probably what shouldn't happen.

You can get the dispatching done by using

abc <- function(x, ...) {
    if (is.null(class(x))) {
	class(x) <- data.class(x)
	abc(x, ...)
    } else
        UseMethod("abc")
}

which (seemingly) works in both languages, at least as long as you
don't have to mess with sys.parent()

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


outer fails with group generic operations on factors (PR#166)
Peter Dalgaard BSA