@ accesses attributes, not just formal slots ?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

@ accesses attributes, not just formal slots ?

Parlamis Franklin
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@names
[1] "A" "B" "C" "D"
 > a@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.

I am still on R 2.2.1.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: @ accesses attributes, not just formal slots ?

Prof Brian Ripley
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@names
> [1] "A" "B" "C" "D"
> > a@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,                  [hidden email]
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel