lua-users home
lua-l archive

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


On 2023-04-28 16:08, Hive Heap wrote:
Thanks for the answer, I have another question regarding the order of
assignments.

If we have a script like this
`a,b,c = d,e,f`, logically we could assume that this is the equivalent
of

a = d;
b = e;
c = f;

but if we look at the bytecode listing, we see that it assigns the
globals on the reverse order (c, b, a)

As far as I know, most languages do the expected output, such as c++,
Java, Python, and even Luau now.
Is there a specific reason for choosing this behavior?

I guess this has something to do with variables appearing on the both
sides of the assignment.

The documentation [1] says

If a variable is both assigned and read inside a multiple assignment,
Lua ensures all reads get the value of the variable before the
assignment.

Consider following code:

a, b = b, a

which actually does swapping. If it is rewritten as

a = b;
b = a;

the result differs obviously.

--
Ziyao

[1]: https://www.lua.org/manual/5.4/manual.html#3.3