lua-users home
lua-l archive

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

On Tue, Aug 9, 2011 at 16:41, Mike Pall <> wrote:
> Lars Doelle wrote:
>>   The semantics of the composite assignment is inconsistent.
> Not a bug. Assignment order is explicitly undefined in Lua:
> --Mike

As I've understood it, when you write something like: a,b = 1,2 you
can view it as two simultaneous assignments. So to write a,a = 1,2
doesn't make a lot of sense - two different values are assigned to a
*at the same time*, so there's no telling which value it ends up with.
(You might even say the correct value is 3, i.e. 1 OR 2, if the
assignments were to literally be simultaneous...)

If you look at section 2.4.3 of the manual though it does state that
x, y = y, x is a valid way to swap variables. Given this and that Lua
seems to be a stack-based language, I suppose this can be read as:
push y; push x; pop y; pop x
i.e. everything after the = is pushed from left to right, then
everything before it is popped right to left.

Sent from my toaster.