From Waclaw.Marcin.Kusnierczyk at idi.ntnu.no  Mon Mar 23 10:56:37 2009
From: Waclaw.Marcin.Kusnierczyk at idi.ntnu.no (Wacek Kusnierczyk)
Date: Mon, 23 Mar 2009 10:56:37 +0100
Subject: [Rd] incoherent treatment of NULL
In-Reply-To: <18887.22657.574884.473000@lynne.math.ethz.ch>
References: <49C74DC3.3090700@idi.ntnu.no>
	<18887.22657.574884.473000@lynne.math.ethz.ch>
Message-ID: <49C75CD5.10903@idi.ntnu.no>

Martin Maechler wrote:
>>>>>> "WK" == Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no>
>>>>>>
>>>>>>             
>     WK> somewhat related to a previous discussion [1] on how 'names<-' would
>     WK> sometimes modify its argument in place, and sometimes produce a modified
>     WK> copy without changing the original, here's another example of how it
>     WK> becomes visible to the user when r makes or doesn't make a copy of an
>     WK> object:
>
>     WK> x = NULL
>     WK> dput(x)
>     WK> # NULL
>     WK> class(x) = 'integer'
>     WK> # error: invalid (NULL) left side of assignment
>
> does not happen for me in R-2.8.1,  R-patched or newer
>
> So you must be using your own patched version of  R ?
>   

oops, i meant to use 2.8.1 or devel for testing.  you're right, in this
example there is no error reported in > 2.8.0, but see below.

>
>     WK> x = c()
>     WK> dput(x)
>     WK> # NULL
>     WK> class(x) = 'integer'
>     WK> dput(x)
>     WK> # integer(0)
>
>     WK> in both cases, x ends up with the value NULL (the no-value object).  in
>     WK> both cases, dput explains that x is NULL.  in both cases, an attempt is
>     WK> made to make x be an empty integer vector.  the first fails, because it
>     WK> tries to modify NULL itself, the latter apparently does not and succeeds.
>
>     WK> however, the following has a different pattern:
>
>     WK> x = NULL
>     WK> dput(x)
>     WK> # NULL
>     WK> names(x) = character(0)
>     WK> # error: attempt to set an attribute on NULL
>   

i get the error in devel.


>     WK> x = c()
>     WK> dput(x)
>     WK> # NULL
>     WK> names(x) = character(0)
>     WK> # error: attempt to set an attribute on NULL
>   

i get the error in devel.

>     WK> and also:
>
>     WK> x = c()
>     WK> class(x) = 'integer'
>     WK> # fine
>     WK> class(x) = 'foo'
>     WK> # error: attempt to set an attribute on NULL
>   

i get the error in devel.

it doesn't seem coherent to me:  why can i set the class, but not names
attribute on both NULL and c()?  why can i set the class attribute to
'integer', but not to 'foo', as i could on a non-empty vector:

    x = 1
    class(x) = 'foo'
    # just fine

i'd naively expect to be able to create an empty vector classed 'foo',
displayed perhaps as

    # speculation
    x = NULL
    class(x) = 'foo'
    x
    # foo(0)

or maybe as

    x
    # NULL
    # attr(, "class")
    # [1] "foo"

vQ



[Rd] incoherent treatment of NULL
Wacek Kusnierczyk