lua-users home
lua-l archive

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




On 11/09/15 08:47 AM, Rodrigo Azevedo wrote:
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
I prefer k1,v1,k2,v2,...

Also:

function(t, ...)
local kandvcount = select('#',...)
for i=1, kandvcount, 2 do
  local k, v = select(i, ...)
end
local kcount = kandvcount / 2 -- number of k = number of k+v / 2, because number of k = number of v.
end

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

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.