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

Greg Minshall minshall at umich.edu
Thu Feb 14 17:57:47 CET 2013


hi.  below is a small test case (hopefully minimal, though i'm still a
bit confused about initializers).

i would have guessed (and maybe i still would have been right) that one
could re-use the name of a generic function for functions with different
numbers of arguments.  in the case below, class A's bB() queries the
status of a single A object, so bB(A) (where here "A" is an instance of
class A, just for "clarity") returns a value.  class B's bB() compares
values in two B objects, and returns the minimum, so bB(B1, B2) are its
methods.

after loading the file, i see the method for A's bB has disappeared (as
measured by showMethods("bB"), as well as trying bB(A).  if i have R
re-parse the setGeneric/setMethod  A's bB(), then B's bB() disappears.

somehow my code, or my model of how things work, is wrong.  any ideas
what am i missing?

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(me) {
    return (new("B", me 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