[Rd] Odd behavior of symbol objects with classes/attributes

luke-tierney at uiowa.edu luke-tierney at uiowa.edu
Mon Jun 17 20:29:44 CEST 2013


On Mon, 17 Jun 2013, Duncan Murdoch wrote:

> On 17/06/2013 1:01 PM, Winston Chang wrote:
>> I've been trying to add classes and attributes to symbol objects
>> (created with quote()), and the behavior is very strange, as
>> illustrated in the examples below.
>> 
>> If symbols aren't meant to have classes and attributes attached to
>> them, then perhaps R should throw errors when you attempt to do it?
>
> I think this is a consequence of another strange property of symbol objects, 
> namely that they are not copied on assignment.  This is also true of 
> environments, NULL, builtin function references, and some rarely encountered 
> types like external pointers and weak references.  You're allowed to assign 
> attributes to all of these other than NULL, but you will find strange things 
> happen if you do it (as demonstrated in your code below).

You are only because we haven't yet gotten around to preventing it --
we will at some point. For now, just don't try to assign attributes to
symbols.

Best,

luke

>
> I thought I remembered reading a list of these in the documentation 
> somewhere, but when I went to look for it just now, I couldn't find it. 
> (Maybe I'm remembering a comment in the source.)  It would be a useful 
> addition.
>
> Duncan Murdoch
>
>> 
>> 
>> # Using str() strips class from object
>> x <- quote(foo)
>> class(x) <- "bar"
>> str(x)
>> # Class 'bar'  symbol foo
>> str(x)
>> # symbol foo
>> 
>> 
>> # Attempting to overwrite doesn't affect class.
>> # str() still strips class from the object.
>> x <- quote(foo)
>> class(x) <- "bar"
>> x <- quote(foo)
>> str(x)
>> # Class 'bar'  symbol foo
>> str(x)
>> # symbol foo
>> 
>> 
>> # Changing class of one object affects other
>> x <- quote(foo)
>> y <- quote(foo)
>> class(x) <- "bar"
>> class(y)
>> # [1] "bar"
>> str(y)
>> # Class 'bar'  symbol foo
>> str(y)
>> # symbol foo
>> str(x)
>> # symbol foo
>> 
>> 
>> # Changing attribute of one object affects other
>> # Unlike with class, str() doesn't cause other attributes to disappear
>> x <- quote(foo)
>> y <- quote(foo)
>> attr(x, "a") <- "bar"
>> str(y)
>> # length 1 foo
>> # - attr(*, "a")= chr "bar"
>> str(y)
>> # length 1 foo
>> # - attr(*, "a")= chr "bar"
>> str(quote(foo))
>> # length 1 foo
>> # - attr(*, "a")= chr "bar"
>> 
>> 
>> -Winston
>> 
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Luke Tierney
Chair, Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   luke-tierney at uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu



More information about the R-devel mailing list