lua-users home
lua-l archive

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

Em sexta-feira, 11 de setembro de 2015, Sean Conner <> escreveu:
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 userdataa

I see. I think one way is to define the syntax

t1[a,b,c] = 1,2,3
t1[a],t1[b],t1[c] = 1,2,3
For this case. This is NOT syntactic sugar (see below).

The "best" annswer is raiser an error.

        t2 = setmetatable({},{__newindex = function(t,k,v) t[k] = v end })
        t2['one','two','three'] = 1,2,3

Using Soni's suggestion about k,v order your code just has the expected behaviour with the assigment
And others arguments ignored as the default behaviou of Lua functions, then, a user mistake.

This question is very different from the first, since in this case (a function metamethod) the behaviour can be more powerfull.

The proposal add functionality that the user need to know how to use, as well as generic functions, Or expect the default behaviour or (maybe) an error. This is compatible with just written code.
  Also, won't this conflict with the proposal for making


as syntactic surgar for


Yes. But the proposed  syntactic sugar can be easily implemented within the proposal. (See your question about t2.)

  -spc (The above wasn't a recent proposal, but I do seem to recall one like
        it sometime in the past few years ... )

Rodrigo Azevedo Moreira da Silva