lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Jul 13, 2010 at 3:21 PM, Petite Abeille <petite.abeille@gmail.com> wrote:

On Jul 13, 2010, at 8:34 PM, Romulo wrote:

>> Usually we consider that what the manual does not say is undefined.
>
> Or overlooked.

Same difference.

No, that's not the same difference.  Some aspects of Lua may simply be overlooked, i.e. the assignments are guaranteed to happen in order to be left to right, and so the behavior can be depended upon after it is documented - it just didn't seem significant to the author of the documentation at the time.  Other aspects may cause demons to fly out of your nose. 

Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
It is not specified.
.....
Usually we consider that what the manual does not say is undefined.
 
This means that, until it's specified, you can't depend on it.  It may be specified in the implementation, (What function generates the bytecode which shows right-to-left assignment?).  Maybe 5.2 could modify the section as follows: (this would also clarify the _expression_ evaluation order):

"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