[Rd] Function calls and UseMethod.
Henrik Bengtsson
henrikb@braju.com
Sun, 15 Jul 2001 23:52:14 -0700
Maybe the following have already been discussed, if not...
BACKGROUND: I am writing a class which should have the method get(). For
this (and a lot of other things) to work I have to redefine the default
get() like this:
if (!exists("get.default")) get.default <- get;
get <- function(obj, ...) {
if (missing(obj) obj <- NULL;
UseMethod("get", obj, ...);
}
and still I am not sure if this is bullet proof. Maybe someone else, i.e. in
a previously loaded library, has already modified the get() method.
SUGGESTION I: Would it be possible to have *all* function calls by default
using the UseMethod approach? What I suggest results in that there is no
need for people to actually write the UseMethod function wrappers, but it
should be done within [R]. Example:
myHelloWorld.default <- function() {
cat("Hello world!\n");
}
and this should be able to be called just as
myHelloWorld()
without having to explicitly write
myHelloWorld <- function(obj, ...) {
if (missing(obj) obj <- NULL;
UseMethod("myHelloWorld", obj, ...)
}
I think this could clean up a lot of the "mess" of overloading and
redefining functions. It would also definitely remove on risk of overwriting
allready existing functions. I can see some problems how to implement this;
for instance, how would [R] know that myHelloWorld is a function call? I
know too little about the internal structure.
SUGGESTION II: If the first approach is not possible, what about "cleaning
up" the [R] code (base etc) and always write like get.default declarations
and not just get? Is there a reason not to?
Thanks
Henrik Bengtsson
150 Panoramic Way
Berkeley, CA 94704
(510) 647-3572
h e n r i k b @ b r a j u . c o m
PS. For some reason I mailed this message to r-announce-request@lists...
before. Sorry about that. DS.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._