How does multiple assignment work?

From Section 4.1 of programming in Lua .

In several assignments, Lua first evaluates all the values ​​and only then does the job. Therefore, we can use multiple assignment to replace two values, as in

x, y = y, x - swap x' fory '

How does assignment work?

+5
source share
1 answer

, , Lua . , . , , , RHS, LHS, Lua.


, , -, . ,

local x,y = 10, 11
x,y = y,x

- (luac -l) Lua 5.2

main <lop.lua:0,0> (6 instructions at 0x9b36b50)
0+ params, 3 slots, 1 upvalue, 2 locals, 2 constants, 0 functions
    1   [1] LOADK       0 -1    ; 10
    2   [1] LOADK       1 -2    ; 11
    3   [2] MOVE        2 1
    4   [2] MOVE        1 0
    5   [2] MOVE        0 2
    6   [2] RETURN      0 1

MOVE ( . lopcodes.h Lua). -, , 0 1 x y, 2 . x y 3 , :

tmp = y -- MOVE 2 1
y = x   -- MOVE 1 0
x = tmp -- MOVE 0 2

, Lua , , ( , , -, - ...). , - , 100%. , , Lua , LuaJIT vs PUC Lua.

+10

All Articles