[R] Definition of = vs. <-
Duncan Murdoch
murdoch at stats.uwo.ca
Wed Apr 1 18:41:43 CEST 2009
On 4/1/2009 11:39 AM, Stavros Macrakis wrote:
> On Wed, Apr 1, 2009 at 10:55 AM, Duncan Murdoch <murdoch at stats.uwo.ca>wrote:
>
>> On 4/1/2009 10:38 AM, Stavros Macrakis wrote:
>>
>>> As far as I can tell from the documentation, assignment with = is
>>> precisely
>>> equivalent to assignment with <-. Yet they call different primitives:
>>>
>>
>> The parser does treat them differently:
>>
>> > if (x <- 2) cat("assigned\n")
>> assigned
>> > if (x = 2) cat("assigned\n")
>> Error: unexpected '=' in "if (x ="
>>
>
> Interesting way of handling the classic C glitch (some of us would say
> "design flaw in C", but...)
>
>
>> The ?"=" man page explains this:
>>
>> " The operator '<-' can be used anywhere,
>> whereas the operator '=' is only allowed at the top level (e.g.,
>> in the complete expression typed at the command prompt) or as one
>> of the subexpressions in a braced list of expressions. "
>>
>> though the restriction on '=' seems to be described incorrectly:
>
>
>> > if ((x = 2)) cat("assigned\n")
>> assigned
>
>
> The restriction is incorrect in many other cases as well, e.g. the following
> are all assignments: function()a=3; if(...)a=3; while(...)a=3; a=b=3 (two
> assignments), and even a*b=3 (parses as assignment, but `*<-` happens not to
> be defined in the default environment).
>
> In fact, the only cases I have found where = does *not* mean assignment is
> in functional or array argument position (f(a=2) and f[a=2]), the following
> contexts with function-like syntax: function(XXX)..., if(XXX)..., and
> while(XXX)...; and for (i in XXX).... Are there any others? Perhaps the
> documentation could be updated?
>
> As to the difference between the operations of the two primitives: see
>> do_set in src/main/eval.c. The facility is there to distinguish between
>> them, but it is not used.
>
>
> So are you saying that it is planned to make = and <- non-synonymous, unlike
> a<-b and b->a, which parse the same and are therefore guaranteed to be
> synonymous?
No, I don't know of any plans like that. That doesn't mean there aren't
any, nor does the current implementation of <- and -> guarantee no
changes there, but I wouldn't expect either to change.
Duncan Murdoch
More information about the R-help
mailing list