Skip to content
Prev 22390 / 63424 Next

Bugs with partial name matching during partial replacement (PR#9202)

This is a rather interesting, but I don't think it is a bug - it is
just things that "you are not supposed to do"... you are assuming
a certain evaluation order of the 4 "$" operators in
" D$ABC[D$M] = D$V[D$M] " as in:

temp1 <- D$M                 # 2nd and 4th
temp2 <- D$V[temp1]          # 3rd
D$ABC[temp1] = temp2         # 1st

What R did was this:

temp4 <- D$ABC         # make reference, expand to D$ABCD , 1st
temp1 <- D$M           # 2nd, and 4th
temp2 <- D$V[temp1]    # 3rd

temp4[temp1] <- temp2  # oh dear, it looks as if we are
D$ABC <- temp4         # trying to write to a reference,
                        # better make a copy instead

R is doing the 4 $'s roughly from left to right, if you have some ideas 
how R works inside. (I am not saying this behavior is a "good" thing,
but at least it is consistent). Basically it is a very bad habit to 
write code that depends on evaluation order of operators at the same
precendence.

The difference in behavior in the two case is probably due to
coercion, (and also how lazy R does make-a-reference versus "oops, you 
seems to try to write to a reference so I better copy it") but
I'll leave you to think about what order R is doing the combination of
the 4 $'s and coercing between types... Basically writing code that 
depends on evaluation order is a bad idea.

c.f. this bit of C code:

i =0;
++i = ++i + ++i;

what value do you think "i" should be?
amaliy1 at uic.edu wrote: