# [R] How to make "<-" generic?

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Sat Jul 26 13:52:17 CEST 2003

```On 26-Jul-03 Peter Dalgaard BSA wrote:
> Peter Dalgaard BSA <p.dalgaard at biostat.ku.dk> writes:
>> > quote("<-"(a,b))
>> a <- b
>
> Adding on to this, I think the point is that assignment bypasses the
> usual *evaluation* rules, even though it is syntactically a binop.
>
> I think it basically has to be so: For one thing, it is kind of
> difficult to check for a signature match without evaluating the
> arguments and the left hand side of an assignment will not in general
> exist at that point.

Need this really be so? (Maybe it has to be in R unless R is unpicked)

For example: suppose the "deep syntax" of <- is "<-"(A,B)

Then:

If A is not there then create it equal to B (surely no problem here).
Otherwise: assign the value of B to A provided this is legal
given what A and B happen to be;
otherwise announce an error.

E.g. if A is a matrix then "<-"(A,2) is OK (bye bye old A),
but if A is a matrix then "<-"(A[2,3],list(one=1,two=2)) is not.

Likewise "<-"(2*A,B) would be illegal. Testing for this would be the
analogue of "lvalues" in C: lvalue is not defined in the formal grammar
of C but is tested for "semantically". Basically an lvalue is an
expression which refers to an "object" (region of memory) in such a
way that it may be altered as well as examined.

But isn't this much like what happens already? E.g.

> "<-"(b,2)
> b
[1] 2
>
> "<-"(2*b,2)
Error: Target of assignment expands to non-language object
>

(Actually you could get away with this in C, because of "pointer
arithmetic" whereby the location of the object pointed to by 2*b has
memory address twice the value of the memory address pointed to by b.
And the best of luck ... ).

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 26-Jul-03                                       Time: 12:52:17
------------------------------ XFMail ------------------------------

```