Skip to content
Prev 15863 / 63461 Next

Overloading methods in R

Hi. Some clarification on R.oo:
I would call them different, rather than old and modern - each kind has its
own use. 

To the details: 
It is the Object class that is related to Java & co; the setMethodS3() and
setConstructorS3() methods are orthogonal and just userfriendly wrappers to
creating functions manually.

The main purpose of Object is provide *reference variables*, with the main
purpose to save memory! This is done by utilizing environment, which is
standard R code, no ugly hacks are used. The Object class defines operators
"$" and "$<-" (and a few others) to access variables within the environment,
which is unique to each instance of class Object. Indeed, there was a
similar feature added in R v1.9.0 (or was it v2.0.0) to the environment
variable; "$" and "$<-" wraps up get() and assign() methods for easy use.
For an object Object, the environment is in a list structure, contrary to
being an environment directly. The reason for this is that attr(), save()
and load() on environments does (did?) not work as you would expect, cf.
https://stat.ethz.ch/pipermail/r-devel/2002-October/025197.html. 

The the Object class defines some other methods to simplify life, and yes,
to imitate Java in the sense that it is convenient to inherit from one
single root Class. It does not allow multiple inheritance (although you can
update the class attributes yourself if you wish too). 

To differ between OOP in Java and S4/Dylan, I prefer to refer to the former
as
class-object-oriented programming (COOP) and the latter as
function-object-oriented programming (FOOP). Then, comparing COOP style with
FOOP style is a bit like comparing peas to apples. I would say that choosing
COOP or FOOP is a design issue that has to do what you are trying to
implement and not a
once-in-a-lifetime/I-want-to-belong-to-this-group-of-people decision. For
what I am working on, I found that higher level implementation, where it is
clear that a method "belongs" to a class, is easier using COOP. Classes for
statistical and mathematical modelling, where functions does not belong to a
specific object, is probably better i FOOP. So, please do not rule out one
for the other!

The setMethodS3() is a wrapper to automatically test for generic and default
functions and create generic functions when needed etc. So

setMethodS3("foo", "MyClass", function(object, ...) { 
  #something
})

replaces things like

if (exists("foo.MyClass", mode="function"))
  warning/stop("Replacing foo.MyClass")

if (exists("foo", mode="function") && !"not a generic function")
  try to rename foo() to foo.default(), but only if foo.default() 
  does not already exists.

... and so on until you can safely write

foo.MyClass <- function(object, ...) {
  # something
}

setConstructorS3() is basically like the above, but it does not create a
generic function nor a class specific method, but a "plain" function

setConstructorS3("MyClass", function(args, ...) {
  # Something
}

to get 

MyClass <- function(args, ...) {
  # Something
}

with check for naming conflicts etc.

Cheers

Henrik Bengtsson
(author of R.oo)