[R] S4 base class

Michael Meyer spyqqqdia at yahoo.com
Thu Oct 17 17:54:28 CEST 2013


By the way, your use of the syntax D::f and B::f suggests that you're 
thinking from a C++ point of view.  That's very likely to lead to 
frustration:  the S4 object system is very different from C++.  Methods 
don't belong to classes, they belong to generics. There is no such thing 
as D::f or B::f, only f methods with different signatures.

Duncan Murdoch 


I am aware of this.
We can probably agree that we should use S4 classes and generic functions to duplicate more usual object oriented architecture as far as possible while remaining conscious of the regrettable differences.

For example we can pretend we are defining a virtual function in class Base by writing:

function(this) standardGeneric("F")

where the code for Base  is, even though it has nothing to do with the class Base. 
We can even use it in other functions "defined in class Base" by writing 

function(this) standardGeneric("G")


which will work on all derived classes which implement F in some fashion:


    # do something appropriate for derived.

With this we can reproduce some semblance of object oriented programming
However, apparently we cannot solve in this manner a common problem of object 
oriented programming (from now on C++ parlance):

Suppose you have a base class "Base" which implements a function "Base::F" 
which works in most contexts but not in the context of "ComplicatedDerived" class
where some preparation has to happen before this very same function can be called.

You would then define

void ComplicatedDerived::F(...){


You can nealry duplicate this in R via 



but it will fail whenever F uses virtual functions (i.e. generics) which are only defined
for derived classes of Base, whereas this is not a problem at all in normal object oriented

This is not a contrived problem but is rather basic.
I wonder if you can do it in R in some other way.

Many thanks,


More information about the R-help mailing list