NextMethod

Paul Gilbert pgilbert@bank-banque-canada.ca
Fri, 16 Apr 1999 12:22:01 -0400


>> One clear moral seems to be don't do anything more inside a
>> generic function than you really need to do.  Keep it *very*
>> simple indeed.
>>
>  I recall JMC saying something like, all generic functions
> should be one line long; a call to the appropriate UseMethod.

It certainly is encouraging to know that others also have been confused by
aspects of UseMethod. I've mostly kept my generic functions to one lined based
on the "keep it simple, stupid" principle. It's nice to have more impressive
references.

The "if it's not broken don't fix it" principle makes me reluctant to consider
the following, but...

In cleaning up parts of my library I realized that many of my old summary
methods actually print things instead of returning an object for a print method.
Some of these use NextMethod, and I've always been even more confused by
NextMethod than by UseMethod. Is the following a good model of how this should
be done:

summary.level1.level2 <- function(object)
{ r <- list("level2 before")
  r <- append(r,NextMethod("summary"))
  r <- append(r,"level2 after")
  class(r) <- "summary.level1.level2"
  r
}

print.summary.level1.level2 <- function(x)
 { cat(x[[1]])
   print(x[[2]])
   cat(x[[3]])
   invisible(x)
 }


z <- 1:3
class(z) <- c("level1.level2", "level1")
summary(z)

TIA
Paul Gilbert

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._