[Rd] S4 initialize or "generating function"

Seth Falcon sfalcon at fhcrc.org
Wed Jan 31 17:31:37 CET 2007


Thomas Petzoldt <Thomas.Petzoldt at tu-dresden.de> writes:
> One odd thing I found was that initialize does obviously not allow to
> incorporate additional named parameters which are not slots.

?!  Does for me:

      setClass("FOO", representation(x="numeric"))
      [1] "FOO"
       
       setMethod("initialize", "FOO",
                 function(.Object, value) {
                     .Object at x <- value * 2
                     .Object
                 })
      [1] "initialize"

      f <- new("FOO", value=1)
      f
      An object of class "FOO"
      Slot "x":
      [1] 2

> In essence I think that one should not use the constructor approach
> (from 2001) anymore, even if the "call is independent of the details
> of the representation".

Sometimes both are useful.  You can have convenience generator
functions for users, but have initialize methods that get called
internally.  This can also be nice in that the internal code can be
lean, while common user-facing code can do lots of error and sanity
checking, etc.

+ seth



More information about the R-devel mailing list