[Rd] Field initialization order bug?

Martin Maechler maechler at stat.math.ethz.ch
Tue Jul 17 19:08:03 CEST 2007


    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



More information about the R-devel mailing list