|
Same difference.
On Jul 13, 2010, at 8:34 PM, Romulo wrote:
>> Usually we consider that what the manual does not say is undefined.
>
> Or overlooked.
Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:It is not specified.
Usually we consider that what the manual does not say is undefined.
"The assignment statement first evaluates all its expressions
from left to right, and only then are the assignments performed, in order from right to left.
Thus the code
i = 3
a = {[3]=30,[4]=40}
i, i, a[i] = i+1, a[i], 20
print(i,a[3],a[4]) -->4 20 40
does not affect a[4]
because the i
in a[i]
is evaluated (to 3)
before it is assigned. Subsequently, a[i] is assigned 20, i is assigned 10, and finally i is reassigned from 10 to 4.
Similarly, the line..."
It appears that Eike's example would be more suitable to this section than the existing piece of code, for which I would argue that any well-behaved programmer would not depend on the assignment order (Just as short-circuit evaluation should be commented when it is relied upon), and would instead refactor it to:
i = 3
a = {[3]=30,[4]=40}
a[i] = 20
i = i + 1
print(i,a[3],a[4]) -->4 20 40
which is useless for the point of this section of the documentation. Eike's code:
local pos = 1
for i=1,#arr do
if arr[i] ~= nil then
arr[pos],arr[i],pos = arr[i],nil,pos+1
end
end
on the other hand, performs a useful function, and is best expressed with the multiple assignment statement.
Kevin Vermeer