Message-ID: <971536df05042120117c4988c9@mail.gmail.com>
Date: 2005-04-22T05:11:48Z
From: Gabor Grothendieck
Subject: Overloading methods in R
In-Reply-To: <BAY17-F332D1436DE28A72C33D140D12D0@phx.gbl>
On 4/21/05, Ali - <saveez@hotmail.com> wrote:
>
>
> >
> >
> >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()
>
> Thanks Gabor! This answers a big part of my question. I am just curious
> why
> something like this doesn't work in S4:
>
> -------------------------
> setGeneric("foo", function(object, ...) standardGeneric("foo"))
>
> foo.NULL <- function(object, ...) {
> args <- list(...)
> classes <- sapply(args, class)
> .Class <- paste(classes, collapse = ".")
> }
>
> foo.default <- function(object, ...) paste("wrong args!")
>
> foo.numeric <- function(object, ...) 2 * ..1
> foo.numeric.numeric <- function(object, ...) ..1 + ..2
> --------------------------
>
>
> On 4/21/05, Ali - <saveez@hotmail.com> wrote:
> >
> >
> > >
> > >
> > >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()
> >
> > Thanks Gabor! This answers a big part of my question. I am just curious
> > why
> > something like this doesn't work in S4:
> >
> > -------------------------
> > setGeneric("foo", function(object, ...) standardGeneric("foo"))
> >
> > foo.NULL <- function(object, ...) {
> > args <- list(...)
> > classes <- sapply(args, class)
> > .Class <- paste(classes, collapse = ".")
> > }
> >
> > foo.default <- function(object, ...) paste("wrong args!")
> >
> > foo.numeric <- function(object, ...) 2 * ..1
> > foo.numeric.numeric <- function(object, ...) ..1 + ..2
> > --------------------------
>
> I am not 100% sure I understand what the question is but I think what
> you are looking for is the fact that ... cannot be part of the signature
> in
> S4. That is ... can be among the method arguments but you can't
> dispatch on those arguments in S4.
>
[[alternative HTML version deleted]]