From Thomas.Petzoldt at tu-dresden.de  Wed Jan 31 18:18:51 2007
From: Thomas.Petzoldt at tu-dresden.de (Thomas Petzoldt)
Date: Wed, 31 Jan 2007 18:18:51 +0100
Subject: [Rd] S4 initialize or "generating function"
In-Reply-To: <m2wt33cg46.fsf@fhcrc.org>
References: <45BFB590.3080405@TU-Dresden.de>
	<m2r6tbdyqc.fsf@fhcrc.org>	<45C0BF94.9020100@tu-dresden.de>
	<m2wt33cg46.fsf@fhcrc.org>
Message-ID: <45C0CF7B.90606@tu-dresden.de>

Seth Falcon wrote:
 > 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:


Your example works but one can not arbitrarily mix slots, other named 
arguments and the default callNextMethod(). The documentation states:

"... 	Data to include in the new object. Named arguments correspond to 
slots in the class definition."


Here is my example (extended from yours):

setClass("FOO", representation(x="numeric", y="numeric"))

setMethod("initialize", "FOO",
          function(.Object, y, value) {
               callNextMethod()
              .Object at x <- value * 2
              .Object
          })

new("FOO", y=1, value=2)

An object of class "FOO"
Slot "x":
[1] 4

Slot "y":
numeric(0)

What is different from what I originally expected. In such cases one has 
to add the arguments to callNextMethod(y=1) explicitly.

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

I see, and for the moment I let the constructors in the package, but 
they only provide now some class specific defaults and then call the 
(inherited) initialize method via new.

Thomas



[Rd] S4 initialize or "generating function"
Thomas Petzoldt