lua-users home
lua-l archive

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


> BTW, is it guaranteed that the assignment order goes from RIGHT to
> LEFT (I verified this in Lua 5.1, 5.2, 5.3, LuaJIT), or is it
> "undefined behavior"?

It is not undefined behavior at all.

The manual does not mention the order of individual assignments in a 
multiple because it is an implementation detail. This allows us to
change the implementation without changing the semantics.

It is like in C, which does not specify the order of evaluation of
expressions in argument lists in function calls.

The semantics of multiple assignment in Lua is explained in the manual:

	The assignment statement first evaluates all its expressions and only
	then the assignments are performed.

and is followed by this example:

	Thus the code
		i = 3
		i, a[i] = i+1, 20
	sets a[3] to 20, without affecting a[4] because the i in a[i] is
	evaluated (to 3) before it is assigned 4.

Again, the manual does not specify the order of evaluation of the expressions.