[Rd] @ accesses attributes, not just formal slots ?

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu May 11 21:53:37 CEST 2006


On Thu, 11 May 2006, Parlamis Franklin wrote:

> Using the '@' operator, I am able to extract a 'names' attribute
> assigned to a formal object.
> However, I can not use the replacement form ('@<-') to assign that
> attribute.
>
> > setClass("foo", representation("numeric"))
> [1] "foo"
> > (new("foo", 1:4)->a)
> An object of class ?foo?
> [1] 1 2 3 4
> > names(a) <- LETTERS[1:4]
> > a at names
> [1] "A" "B" "C" "D"
> > a at names <- LETTERS[5:8]
> Error in checkSlotAssignment(object, name, value) :
> 	"names" is not a slot in class "foo"
>
> I don't know that this asymmetry will ever cause a problem, but I
> just wanted to note it because it appears from the help pages
> 'slot {methods}'  and 'slotOp {base}' that the '@' operator is only
> intended to provide access to defined slots.

Yes, it is intended so.  It is a side effect of the current implementation 
that @ will also access attributes, but that may change.  Note that the
help page for @ says

      These operators support the formal classes of package 'methods'.
      See 'slot' for further details. Currently there is no checking
      that the object is an instance of a class.

so this should not come as a surprise to you.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list