From sfalcon at fhcrc.org  Wed Jan 31 17:31:37 2007
From: sfalcon at fhcrc.org (Seth Falcon)
Date: Wed, 31 Jan 2007 08:31:37 -0800
Subject: [Rd] S4 initialize or "generating function"
In-Reply-To: <45C0BF94.9020100@tu-dresden.de> (Thomas Petzoldt's message of
	"Wed, 31 Jan 2007 17:11:00 +0100")
References: <45BFB590.3080405@TU-Dresden.de> <m2r6tbdyqc.fsf@fhcrc.org>
	<45C0BF94.9020100@tu-dresden.de>
Message-ID: <m2wt33cg46.fsf@fhcrc.org>

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



[Rd] S4 initialize or "generating function"
Seth Falcon