Re: [Rd] Constructor blah() vs. as.blah()

From: Henrik Bengtsson <hb_at_stat.berkeley.edu>
Date: Fri, 15 Aug 2008 14:00:25 -0700

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 constructor function.

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

/Henrik

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?
>
> Jeff
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri 15 Aug 2008 - 21:06:17 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 16 Aug 2008 - 17:36:44 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive