[Rd] Constructor blah() vs. as.blah()
hb at stat.berkeley.edu
Fri Aug 15 23:00:25 CEST 2008
This thread is going to be a lot about matter of tastes, but at least
I would think of blah() as a constructor function and as.blah() as a
coerce function. There should always be one constructor function,
but providing coerce functions is optional.
Furthermore, the constructor function should be as basic as possible
and have minimal dependencies. For instance, if you create your own
class for bitmap images, then you probably want to be able to extract
a bitmap object of your class from say pixmap or EBImage image
objects. In the future there might be other image classes showing up.
To keep you code and API clean, it is better create as.blah() methods
for those classes and keep them out of the more light-weight
These thoughts applies also to S4 classes, where one should avoid
having users to call new(), but instead provide a constructor
function. It should be hidden as far as possible whether S3 or S4 is
used. If you one change from an S3 to an S4 implementation of you
class and methods, ideally existing scripts using you code should look
the same as before and after.
Hope this helps
On Fri, Aug 15, 2008 at 9:34 AM, <jhallman at frb.gov> wrote:
> When should we use one versus the other? If I'm designing an S3 class
> "blah", should I just implement
> blah <- function(x, ...) UseMethod("blah")
> and then a bunch of blah.whatever() functions, including blah.default()?
> Or should I do
> as.blah <- function(x, ...) UseMethod("as.blah")
> with a bunch of methods for it? Or do both?
> R-devel at r-project.org mailing list
More information about the R-devel