On Tue, 2006-03-07 at 16:24 +0000, Barry Rowlingson wrote: > Wouter Buytaert wrote: > > > The resulting R function doesn't seem very complicated. I'm a newbie > > though, so all recommendations welcome: > > > > topidx <- function(map) { > > > > if(!(class(map) == "SpatialGridDataFrame")) > > printf("Map should be of SpatialGridDataFrame class") > > > > Recommendation number 1 - read all about Object-Oriented Programming! > > You shouldn't have a function that tests the class of an object, you > should write a generic function and a method for your class.
That's a rather odd aspect of S methods -- even if you only want to call the function for a single class, you have to create a generic function. In cases where I'm not wanting polymorphic behavior (the function really only makes sense for a single class), I tend to write ordinary functions and either test for class membership or just document that the passed object should be of the appropriate class. When the function will be called on multiple classes, then it is worth defining a generic function. Making everything generic is too much overhead. THK -- Timothy H. Keitt Assistant Professor http://www.keittlab.org/ http://www.utexas.edu/directory/index.php?q=Keitt _______________________________________________ R-sig-Geo mailing list R-sig-Geo@stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo