Quote
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:
setGeneric("F",
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
setGeneric("G",
function(this) standardGeneric("G")
)
setMethod("G",
signature(this="Base"),
definition=function(this){
????F(this)
})
which will work on all derived classes which implement F in some fashion:
setMethod("F",
signature(this="Derived"),
definition=function(this){
????# 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(...){
????preparation();
????Base::F();
}
You can nealry?duplicate this in R via
setMethod("F",
signature(this="ComplicatedDerived"),
definition=function(this){
????preparation(this)
????F(as(this,"Base"))
})
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
languages.
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,
Michael