Skip to content
Prev 15869 / 63461 Next

Overloading methods in R

[That one got stuck in my spam filter; I've sent you a reply just before
seing this one. I'll paste th?t answer here too, if someone else is interest
or can add to the topic.]

Unfortunately, there is no immediate solution to this in the R language;
neither in S3 nor S4. The problem as is basically that you can only have one
generic function and that all methods dispatch by this generic function are
required to have the same arguments. I've been trying to get a discussion on
this problem, because I see the problem of two people creating two
independent package containing generic functions of the same name but
different arguments - that won't work today (at least not well). [namespaces
help a bit, though]

However, in S3 you can create a "generic" generic function by not specifying
arguments but only '...' - this way any methods can take any arguments (and
you don't force your argument names onto other developer's). Example:

foo <- function(...) UseMethodS3("foo")

foo.ClassA <- function(object, ...) { <code> }
foo.ClassB <- function(x, y, ...) { <code> }

and so on. This will *not* solve your problem of have overloaded methods in
the same class [using COOP thinking]. That is not possible to do, what I
understand. The only way I can think of is to have an ad hoc method which in
turn checks the arguments and dispatch on them. 

foo.ClassA <- function(...) {
  args <- list(...);
  # Investigate names(args) and lapply(args, FUN=class) for further
dispatching
  # to "private" methods .foo_x.ClassA(...), .foo_x_y.ClassA(...). 
}

(This is inline with what Gabor Grothendieck outlined.) However, I am not
sure if the further methods dispatching with NextMethod() will work. You
probably want to define generic functions for .foo_x() and .foo_x_y() so
they in turn can be overloaded by subclasses.

If you figure a good schema for the above, maybe it can be made automatic so
that one can have a

setOverloadMethodS3("foo", "ClassA", function(object, ...) { <code> })
setOverloadMethodS3("foo", "ClassA", function(x, y, ...) { <code> })

to do the above. That would require some inspection of the arguments, but
that is not hard using formals().

So, the above is just a sketch that might or might not work. I think you
best shot is indeed to use S3, because it is a bit more flexible; S4 is
probably too rigid for this purpose.

BTW, I think it would be nice if you can develope an easy way to define
wrappers for C++ and other similar language! [Just don't reinvent the
wheel.]
I certainly hope S3 will not be obsolete on day and if it is every planned I
certainly would to see a real and sensible open discussion on this long
before being done! [You already know my standpoint here; S3 and S4
complement, not fight, each other]
So answered aboved.
Indeed, I know that Nathan Whitehouse worked on this, see
http://maths.newcastle.edu.au/~rking/R/devel/03b/0584.html. I do not know
the current status of it, but I think he wrote some "white papers" on the
topic. I don't know where and how many they are. There might be something in
the http://www.rho-project.org project.
 
Best wishes

Henrik