[R] How to get setGeneric() to work?

Henrik Bengtsson hb at maths.lth.se
Sun Jan 6 22:53:38 CET 2002


Dear all, I can't see how setGeneric() works. I know perfectly well how to
define

 library(methods);

 setClass("Square", representation(side="numeric"));

 # Will become the default method.
 getArea <- function(object) {
   stop(paste("Method getArea() is not defined for this class:",
      data.class(object)));
 }

 setMethod("getArea", "Square", function(object) object at side^2);

 # TEST
 sq <- Square(8);
 area <- getArea(sq);

However, when use setGeneric() explicitly without defining getArea();

 setGeneric("getArea", function(object) {
   stop(paste("Method getArea() is not defined for this class:",
              data.class(object)));
 })

the getArea(sq) call will give:

 "Error in getArea(sq) : Method getArea() is not defined for this class:
Square"

which is not what I expected (I would expect the same as the code above). I
believe I miss something, but what?

Thanks

Henrik Bengtsson

Dept. of Mathematical Statistics @ Centre for Mathematical Sciences
Lund Institute of Technology/Lund University, Sweden (+2h UTC)
Office: P316, +46 46 222 9611 (phone), +46 46 222 4623 (fax)
h b @ m a t h s . l t h  . s e
http://www.maths.lth.se/matstat/staff/hb/


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list