[R] 2 setGeneric's, same name, different method signatures

Greg Minshall minshall at umich.edu
Thu Feb 14 22:12:43 CET 2013


i'm answering my own question: 

1.  setGeneric's override (wipe out, really) previous ones.  (this is
pointed out in section 5.3 of "A (Not So) Short Introduction to S4
Object Oriented Programming in R" by Christophe Genolini.)

2.  the *names* of the formals are important.

3.  one can specify a method that takes a subset of the formals declared
in the setGeneric.

so, in my example, changing "me" to "b1" in A's bB(), allows A to use
B's setGeneric().

for completeness, below is my code that works (or, seems to!).

if anyone knows, in #3 above, how A can specify that "no b2 is
allowed!", i'd be curious.

cheers, Greg Minshall
----
setClass("A",
         representation(
           x="numeric"));

setMethod(
  f="initialize",
  signature="A",
  definition=function(.Object) {
    .Object at x <- 23;
    return(.Object)
  });

setGeneric("bB", function(me) standardGeneric("bB"));
setMethod(
  "bB",
  signature("A"),
  definition=function(b1) {
    return (new("B", b1 at x))});

setClass("B", representation(
  bx="numeric"));

setMethod(
  "initialize",
  signature("B"),
  definition=function(.Object, x) {
    .Object at bx <- x;
    return(.Object);
  });

setGeneric("bB", function(b1, b2) standardGeneric("bB"));
setMethod(
  "bB",
  signature("B", "B"),
  definition=function(b1, b2) {
    return(new("B", min(b1 at bx, b2 at bx)))});



More information about the R-help mailing list