[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [PROPOSAL] multiple keys/values with function metamethod
- From: Sean Conner <sean@...>
- Date: Fri, 11 Sep 2015 17:55:51 -0400
It was thus said that the Great Rodrigo Azevedo once stated:
> 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.
What would the exepected results be for the following code?
-- regular table with no metatable
t1 = {}
t1['one','two','three'] = 1,2,3
-- because I can see this happening in real code
-- or unmodified C modules (think userdata)
t2 = setmetatable({},{__newindex = function(t,k,v) t[k] = v end })
t2['one','two','three'] = 1,2,3
Also, won't this conflict with the proposal for making
t[1,2,3]
as syntactic surgar for
t[1][2][3]
-spc (The above wasn't a recent proposal, but I do seem to recall one like
it sometime in the past few years ... )