lua-users home
lua-l archive

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


This is not a syntactic sugar! It is completely different. I am not assigment to diffent keys, but to a multiple keys. This cant be done with table as metamethod because only functions accept multiple arguments.  The body of index and newindex is an arbitrary function not a simple replacement of multiple assigments.

Em 11/09/2015 10:21, "Dirk Laurie" <dirk.laurie@gmail.com> escreveu:
2015-09-11 13:47 GMT+02:00 Rodrigo Azevedo <rodrigoams@gmail.com>:

> a,b,t[k1,k2,k3] = v1,v2,v3,v4,v5

This is not a question of metamethods, it is a question of allowing
lists as indices, so t[k1,k2,k3] is syntactic sugar for t[k1],t[k2],t[k3]
on either side of an assigment operator.

I am pretty sure this must have been suggested before (if so, we're
not gonna get it), but like you, am not sufficiently motivated to search
the list archives for that precedent.

It is true that the current bytecode in this situation could do with
some optimization. Far too many GETTABUP's.

$ luac -l -l -p -
t[1],t[2],t[3] = 10,20,30
b = {t[3],t[1],t[2]}

main <stdin:0,0> (18 instructions at 0x6b3840)
0+ params, 5 slots, 1 upvalue, 0 locals, 8 constants, 0 functions
    1    [1]    GETTABUP     0 0 -1    ; _ENV "t"
    2    [1]    GETTABUP     1 0 -1    ; _ENV "t"
    3    [1]    GETTABUP     2 0 -1    ; _ENV "t"
    4    [1]    LOADK        3 -5    ; 10
    5    [1]    LOADK        4 -6    ; 20
    6    [1]    SETTABLE     2 -4 -7    ; 3 30
    7    [1]    SETTABLE     1 -3 4    ; 2 -
    8    [1]    SETTABLE     0 -2 3    ; 1 -
    9    [2]    NEWTABLE     0 3 0
    10    [2]    GETTABUP     1 0 -1    ; _ENV "t"
    11    [2]    GETTABLE     1 1 -4    ; 3
    12    [2]    GETTABUP     2 0 -1    ; _ENV "t"
    13    [2]    GETTABLE     2 2 -2    ; 1
    14    [2]    GETTABUP     3 0 -1    ; _ENV "t"
    15    [2]    GETTABLE     3 3 -3    ; 2
    16    [2]    SETLIST      0 3 1    ; 1
    17    [2]    SETTABUP     0 -8 0    ; _ENV "b"
    18    [2]    RETURN       0 1
constants (8) for 0x6b3840:
    1    "t"
    2    1
    3    2
    4    3
    5    10
    6    20
    7    30
    8    "b"
locals (0) for 0x6b3840:
upvalues (1) for 0x6b3840: