From ggrothendieck at gmail.com  Fri Apr 22 00:49:03 2005
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Fri Apr 22 00:55:52 2005
Subject: [Rd] Overloading methods in R
In-Reply-To: <BAY17-F69C404BCA375CB5704ABCD12C0@phx.gbl>
References: <4267FACC.9050001@blackmesacapital.com>
	<BAY17-F69C404BCA375CB5704ABCD12C0@phx.gbl>
Message-ID: <971536df050421154970feeadf@mail.gmail.com>

On 4/21/05, Ali - <saveez@hotmail.com> wrote:
> 
> >>
> >>- How to overload methods in classes created by R.oo package?
> >[snip]
> >
> >Maybe you missed it in the flurry of messages, but did the idea suggested
> >by Gabor Grothendick not suit your needs?
> 
> I had to abstract the question in a setntence this time to prevent it to be
> missed again 'in the flurry of messages'.
> 
> Gabor Grothendick's example does work for S3 and your own example does work
> for S4 but none of them answer the problem I declared a few times. Both
> examples work fine for an 'specific' case. I am looking for a general
> solution to use it with a parser as part of an automatic  wrapper.
> 
> When wrapping some arbitrary C++ classes, we don't know
> 
> - how many functions are overloaded in a class
> - how many arguments each function has
> - what class is each argument of each function
> 
> Implementing the wrapper in the way that you and Gabor Grothendick suggest
> requires a lot of 'if' and 'missing' making it too elaborated.



Here is another example which hopefully shows that
there is no limitation on number of arguments,
number of methods and classes.    Here we have
methods with one, two and three arguments and a
variety of classes.

Note that this and the previous examples are not
intended to be complete finished solutions to your
problem but are only illustrative to give you the
idea how to proceed.  Generalizing them should not
be too complex nor inelegant.  Dealing with
vectors of arguments is not that much harder than
dealing with individual arguments in a vector
oriented language like R.


f <- function(...) UseMethod("f", NULL)

f.NULL <- function(...) {
	args <- list(...)
	classes <- sapply(args, class)
	.Class <- paste(classes, collapse = ".")
	NextMethod("f", ...)
}

f.numeric <- function(...) 2 * ..1 
f.numeric.numeric <- function(...) ..1 + ..2
f.character.numeric.Date <- function(...) {
   args <- list(...)
   paste(args[[1]], args[[2]], format(args[[3]], "%Y-%m-%d"))
}
f.default <- function(...) print(list(...))


f(1)   # 2
f(1,2) # 3
f("a", 23, Sys.Date()) # "a 23 2005-04-21"
f()    # list()


[Rd] Overloading methods in R
Gabor Grothendieck