Dear Development Group,
In trying to answer the subject question myself, I found this thread
initiated by Tony from five years ago:
http://tolstoy.newcastle.edu.au/R/help/05/12/18169.html. I recently stumbled
upon the following S4 answer to Tony's question:
setClass("fooWfcn",
representation(dat1="vector",
dat2="vector",
fn1="function",
fn2="function"),
prototype=prototype(dat1=0:10, dat2=10:20)
)
setMethod("initialize", "fooWfcn", function(.Object, ...) {
.Object <- callNextMethod()
.Object@fn1 <- function(x) { return(x - mean(.Object@dat1)) }
.Object@fn2 <- function(x) { mean(.Object@dat2) }
.Object
})
foo <- new("fooWfcn")
foo@fn1(0) # -5
foo@fn2() # 15
foo2 <- new("fooWfcn", dat1 = 11:20)
foo2@fn1(0) # -15.5
(If this is well-known, I apologize, but I could not find it anywhere.)
Before going too far down the road under the assumption that this S4
self-referencing-via-initialize mechanism will always work (so far so good,
in broad contexts) I would be grateful for some confirmation from the
development team that this "happy accident" will not suddenly
disappear. :-(
Thank you,
Dan Murphy
[[alternative HTML version deleted]]