Problem
In R, you can assign an attribute like so:
attr(x, "foo") <- "bar"
I get what this does, but I'm trying to understand how this statement breaks down into more fundamental pieces of the R language.
Thoughts
While attr
is a function, it appears to also be a part of the fundamental language grammar, since you cannot do this:
t = attr
t(x, "bar") <- "baz" # gives an error
The most similar syntax I can think of is in PHP, where you can assign by "calling" the list
"function" like this:
list($a, $b) = array(1, 2);
I understand the above line to have the following grammar:
list_assignment:
list(<var>...) = <expression>;
It would seem to make sense that in R, the grammar is defined similarly:
attr_assignment:
attr(<var>, <expression>) <- <expression>;
But you can also write this:
x = `attr<-`(x, "bar", "baz")
Which seems that it would require one of the following:
- Special support for
`attr<-`(<var>, <expr>, <expr>)
in the grammar - A real function named
attr<-
which does the same asattr(...) <-
under the hood
Summary
How should this attr
assignment should be thought about in terms of simpler language elements? What about `attr<-`
? Additionally, how are these implemented within the R parser?