[Rd] slot(obj, "nosuch") documentation question

Paul Roebuck plroebuck at mdanderson.org
Sat Jun 7 10:44:31 CEST 2008


Using slot() on object (or "@") and using a nonexistent
slotname returns an error (see example code).

R> setClass("foobar", representation=list(a="numeric"))
[1] "foobar"
R> foobar <- new("foobar", a=5)
R> foobar at a
[1] 5
R> foobar at b
Error: no slot of name "b" for this object of class "foobar"

The details section of manpage has a paragraph that
seems to agree with above:

    Unlike attributes, slots are not partially matched,
    and asking for (or trying to set) a slot with an
    invalid name for that class generates an error.

But then the last paragraph in that same section seems to
suggest otherwise.

    Currently the @ extraction operator and slot function
    do no checking, neither that object has a formal class
    nor that name is a valid slot name for the class. (They
    will extract the attribute of the given name if it
    exists from any R object.) The replacement forms do
    check (at least if check=TRUE).

Are these two paragraphs not in partial conflict with each
other? I had initially expected (my initial bottom-up
quick glance only read last paragraph) to get NULL from
invalid slotname.

This is in reference to writing method that might be
passed an object created by previous version of software,
and new slots were added to class in later revisions.

For such situations, is the norm then to do
"slotNames(obj) %in% <slotname>" prior to accessing any
slotname from extended class?


TIA


R version 2.7.0 Patched (2008-06-04 r45830)

----------------------------------------------------------
SIGSIG -- signature too long (core dumped)



More information about the R-devel mailing list