From maechler at stat.math.ethz.ch  Tue Jul 17 19:08:03 2007
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Tue, 17 Jul 2007 19:08:03 +0200
Subject: [Rd] Field initialization order bug?
In-Reply-To: <469CB73A.7030006@biostat.ku.dk>
References: <p0624081dc2c19b42aa26@[128.165.72.98]>
	<Pine.LNX.4.64.0707170821220.30535@auk.stats>
	<469CB73A.7030006@biostat.ku.dk>
Message-ID: <18076.63347.601933.898626@stat.math.ethz.ch>


    PD> Prof Brian Ripley wrote:

       PBR> On Mon, 16 Jul 2007, Daniel Wilhelm wrote:

	 >>> I believe that I may have found a bug in R. The top code sample gives
      PBR> You have 'merely' found a bug in your understanding.
      PBR> What type did you expect 'a' to be?  If you expected
      PBR> a list, that is not what happens in the first
      PBR> example, and you need

      PBR> a <- list()
      PBR> 
      PBR> or, better,
      PBR> 
      PBR> a <- vector("list", 2)

    PD> To be precise, you need

    PD> a <- vector("list", 2) ; names(a) <- c("field1", "field2")

    PD> or you end up with a 4-element list.

yes;  and consequently, a more readable solution would start saying

   a <- list(field1 = NULL, field2 = NULL)

or equivalently (and maybe nicer looking):

   a <- list(field1 = {}, field2 = {})


Martin Maechler, ETH Zurich



[Rd] Field initialization order bug?
Martin Maechler