[Rd] incoherent treatment of NULL
Martin Maechler
maechler at stat.math.ethz.ch
Mon Mar 23 15:44:16 CET 2009
>>>>> "WK" == Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no>
>>>>> on Mon, 23 Mar 2009 10:56:37 +0100 writes:
WK> 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 ?
>>
WK> oops, i meant to use 2.8.1 or devel for testing. you're right, in this
WK> example there is no error reported in > 2.8.0, but see below.
ok
[...... omitted part no longer relevant ........]
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
>>
WK> i get the error in devel.
Yes, NULL is NULL is NULL ! Do read ?NULL ! [ ;-) ]
more verbously, all NULL objects in R are identical, or as the
help page says, there's only ``*The* NULL Object'' in R,
i.e., NULL cannot get any attributes.
WK> x = c()
WK> dput(x)
WK> # NULL
WK> names(x) = character(0)
WK> # error: attempt to set an attribute on NULL
>>
WK> i get the error in devel.
of course!
[I think *you* should have noticed that NULL and c() *are* identical]
WK> and also:
>>
WK> x = c()
WK> class(x) = 'integer'
WK> # fine
"fine" yes;
here, the convention has been to change NULL into integer(0);
and no, this won't change, if you find it inconsistent.
WK> class(x) = 'foo'
WK> # error: attempt to set an attribute on NULL
>>
WK> i get the error in devel.
No, not if you evaluate the statements above (where 'x' has
become 'integer(0)' in the mean time).
But yes, you get in something like
x <- c(); class(x) <- "foo"
and I do agree that there's a buglet :
The error message should be slightly more precise,
--- improvement proposals are welcome ---
but an error nontheless
WK> it doesn't seem coherent to me: why can i set the class,
you cannot set it, you can *change* it.
WK> but not names
WK> attribute on both NULL and c()? why can i set the class attribute to
WK> 'integer', but not to 'foo', as i could on a non-empty vector:
WK> x = 1
WK> class(x) = 'foo'
WK> # just fine
mainly because 'NULL is NULL is NULL'
(NULL cannot have attributes)
WK> i'd naively expect to be able to create an empty vector classed 'foo',
yes, but that expectation is wrong
WK> displayed perhaps as
WK> # speculation
WK> x = NULL
WK> class(x) = 'foo'
WK> x
WK> # foo(0)
WK> or maybe as
WK> x
WK> # NULL
WK> # attr(, "class")
WK> # [1] "foo"
WK> vQ
WK> ______________________________________________
WK> R-devel at r-project.org mailing list
WK> https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list