[Rd] incoherent treatment of NULL

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Mon Mar 23 10:56:37 CET 2009


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



More information about the R-devel mailing list