From saveez at hotmail.com  Wed Apr 20 17:45:09 2005
From: saveez at hotmail.com (Ali -)
Date: Wed Apr 20 17:45:19 2005
Subject: [Rd] Overloading methods in R
In-Reply-To: <426674E5.8030705@acm.org>
Message-ID: <BAY17-F3487AE8B0461A5BEC97D08D12B0@phx.gbl>

Thanks a lot Tony. I am trying to apply the overloading to the methods 
created by R.oo package and, unfortunately, R.oo uses S3-style classes; so I 
cannot use the features of S4 methods as you described. On the other hand, I 
caouldn't find a decent OO package which is based on S4 AND comes with the 
official release of R.

Assume we have this class created using R.oo:

-------------------------
require(R.oo)

setConstructorS3("SomeClass",
    function(...)
    {
        extend(Object(), "SomeClass");
    }
);

setMethodS3(
    "fun",
    "SomeClass",
    function(this, x, ...)
    {
        paste(x);
    }
);
-------------------------

Now if we overload the method by:

-------------------------
setMethodS3(
    "fun",
    "SomeClass",
    function(this, x, y, ...)
    {
        paste(x, y);
    }
);
-------------------------

then R.oo overwrites the method 'fun' with the new definition. As the 
overloaded methods share the same class, it is not possible to use the 
function overloading method as you described.

I hope there is some other way to handle this problem.



>One thing to realize is that "methods" in R are very different to those in 
>other common "object-oriented" languages such as C++, Java, or Python.  One 
>difference is that methods are not intimately associated with a class 
>definition -- the method dispatch system pulls disparate pieces of 
>information together.  Another difference is that the choice of which 
>method to invoke can depend on all arguments, not just the first.
>
>Yet another thing to realize is that R has two class systems (commonly 
>known as something like "S3" and "S4" methods and classes).  Each has its 
>own way of defining classes, generic functions and methods.  The S4 class 
>system offers more control over which method is called.  Consult the "Green 
>Book" for details of the S4 system.  (See the FAQ for full details of the 
>"Green Book").
>
>Here's an example of something like what you are asking for, using the S4 
>class system.  Note that this example doesn't involve the definition of any 
>classes, just a generic and methods for some standard classes. Also note 
>that a generic and its methods should have the same argument names.
>
> > setGeneric("foo", function(x,y) standardGeneric("foo"))
>[1] "foo"
> > setMethod("foo", c("ANY", "missing"),
>+     function(x,y) "x:any y:missing")
>[1] "foo"
> > setMethod("foo", c("numeric", "numeric"),
>+     function(x,y) "x:numeric y:numeric")
>[1] "foo"
> > setMethod("foo", c("missing", "numeric"),
>+     function(x,y) "x:missing y:numeric")
>[1] "foo"
> > foo(1,2)
>[1] "x:numeric y:numeric"
> > foo(1)
>[1] "x:any y:missing"
> > foo(y=2)
>[1] "x:missing y:numeric"
> > foo(y="bar")
>Error in foo(y = "bar") : no direct or inherited method for function 'foo' 
>for this call
> >
>
>(You can execute the above commands under Windows by copying the entire 
>transcript to the clipboard, and then using the "Paste commands only" menu 
>item under the "Edit" menu in the "R console" window.)
>
>hope this helps to at least get you started,
>
>Tony Plate
>


[Rd] Overloading methods in R
Ali -