lua-users home
lua-l archive

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


We can use functions as metamethods,

t[k] call __index = function(t,k) end
or
t[k] = v call __newindex = function(t,k,v) end

but in Lua functions can have multiple parameters as well as real multiple return values.

Proposal: extend the syntax to

t[k1,k2,k3] to call __index = function(t,k1,k2,k3) end
and
t[k1,k2,k3] = v1,v2,v3 to call __newindex = function(t,k1,k2,k3,v1,v2,v3) end
-- maybe with local _ENV variables to specify the number of k and v

and to keep things consistent with the current syntax of assignments

a,b,t[k1,k2,k3] = v1,v2,v3,v4,v5
equals
a = v1; b=v2; t[k1,k2,k3] = v3,v4,v5
and
a,t[k1,k2,k3],b = v1,v2,v3,v4,v5
equals
a=v1; t[k1,k2,k3] = v2; b = v3

I think this really improve the syntax/simplify userdata use cases and keep things consistent with Lua functions capabilities.

Simple examples:

"multidimensional tables"
u[v1,v2] = v4
will be much faster (with only one copy of all values to the stack) then u[v1][v2] = v4, with two function calls and consequent copies to the stack, memory allocation etc.

"simple archive"
arq["Name","Country"] = "Rodrigo","Brazil"
is more readable (and possible fast) than
arq.Name = "Rodrigo"
arq.Country = "Brazil"

Of course the C side must change, but if you are smart enough to write a C module then you can adapt yourself.


--
Rodrigo Azevedo Moreira da Silva