methods function

Jonathan Rougier J.C.Rougier@durham.ac.uk
Tue, 22 Oct 2002 17:01:51 +0100


Hi everyone,

I've noticed that the methods function is incomplete.  The obvious
example is if I write a function "$.foo", it is not picked up by
methods:

> "$.foo" <- function(object, name) get(name, envir=object[[1]])         
> "$<-.foo" <- function(object, name, value) {assign(name, value, envir=object[[1]]); object}
> happy <- structure(list(new.env()), class = "foo")
> happy$dog <- "fred"
> happy$dog
[1] "fred"
> methods("$")
character(0)

This happens to be an easy one to fix: simply replace the first argument
to gsub in the final line with "([.[$])".  But then I thought about the
bigger picture.  Don't we need to escape all (well, almost all) special
characters in the "name" variable inside methods?  For example, I might
want to class an object as "dopey$", which would not be wrong.  This
would mean a function like

"methods" <- function (generic.function, class) 
{
    an <- lapply(seq(along = (sp <- search())), ls)
    names(an) <- sp
    if (!missing(generic.function)) {
        if (!is.character(generic.function)) 
            generic.function <- deparse(substitute(generic.function))
        name <- paste(generic.function, ".", sep = "")
        name <- paste("^", gsub("([.[$])", "\\\\\\1", name), sep="")
    }
    else if (!missing(class)) {
        if (!is.character(class)) 
            class <- paste(deparse(substitute(class)))
        name <- paste(".", class, sep = "")
        name <- paste(gsub("([.[$])", "\\\\\\1", name), "$", sep="")
    }
    else stop("must supply generic.function or class")
    grep(name, unlist(an), value = TRUE)
}

but with more special characters, eg "^", inside the first arguments to
gsub.  Is this worth thinking about?

Cheers, Jonathan.

-- 
Jonathan Rougier                       Science Laboratories
Department of Mathematical Sciences    South Road
University of Durham                   Durham DH1 3LE
tel: +44 (0)191 374 2361, fax: +44 (0)191 374 7388
http://www.maths.dur.ac.uk/stats/people/jcr/jcr.html
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._