R's UseMethod() does not dispatch on changed class() (PR#167)
ripley@stats.ox.ac.uk
ripley@stats.ox.ac.uk
Thu, 15 Apr 1999 00:37:26 +0200
On 15 Apr 1999, Peter Dalgaard BSA wrote:
> Robert Gentleman <rgentlem@hsph.harvard.edu> writes:
>
> > Do you really want to have a language that accepts any change in x?
> > It is rather hard to see how it's object oriented then!
> > Or, alternatively do you really want to see if the only thing that
> > has changed about x is its class and that will be ok?
> > What about the other arguments?
> > The whole point of UseMethod is that we got an object x, with a
> > particular class and now we're going to do the right thing.
> > If you wanted to do the right thing with an object of a different
> > class then you need to do that before you call UseMethod.
>
> I'm inclined to agree here. On the other hand, what might be the case
> is that you could make class<- be a little more careful with its
> argument so that UseMethod would see the modified x. Sneaky, but
> compatible, and *perhaps* not very hard to do. How often is this kind
> of construct (class(x)<-foo;UseMethod()) used in actual S code?
Often by certain people with a massive presence!
We have indeed been here before. In our lda function we do
class(x) <- data.class(x)
UseMethod("lda")
That fails to use the new class in R, and according to `your' reading of
the White book (but not mine nor apparently the author of S's) should do
so. `You' suggested
UseMethod("lda", x, ...)
which works in some versions of S-PLUS 3.x but crashes the DEC Alpha
version and works on no version of S-PLUS 5.x. Hence I have
lda <- function(x, ...)
{
if(is.null(class(x))) class(x) <- data.class(x)
#ifdef R
UseMethod("lda", x, ...)
#ifdef SP3
UseMethod("lda")
}
and in SP5 I do something with new-style classes.
> - and Joe User generally gets in trouble with methods anyway...
and not just Joe User....
Brian
--
Brian D. Ripley, ripley@stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._