lua-users home
lua-l archive

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


On Thu, Apr 10, 2014 at 3:41 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 2014-04-10 8:16 GMT+02:00 Dirk Laurie <dirk.laurie@gmail.com>:
>> 2014-04-10 6:50 GMT+02:00 Coroutines <coroutines@gmail.com>:
>>
>>> After thinking about this for a while I think it would be okay if
>>> object literals were also allowed instead of only valid identfiers:
>>>
>>> 1, 2, 3 in { 'a', 'b', 'c' } --> return 'a', b', 'c'
>>
>> I'm happier with the notion that `x,y,z in tbl` is just a synonym for
>> `tbl.x, tbl.y, tbl.z`. The above would be better described by
>> { 'a', 'b', 'c' }[1,2,3] -- but let's not hijack this thread.
>
> Suppose that the notation tbl[i,j,k] meant tbl[i],tbl[j],tbl[k], not
> as a syntactic sugar, but as genuine multiple indexing overridable
> by metamethods.
>
> __index(tbl,key1,key2,...).
> __newindex(tbl,key1,val1,key2,val2,...).
>
> Invoked when tbl is not a table, when key1 is absent, and when more
> than one key is supplied.
>
> People who want tbl[i,j] to mean matrix-like indexing can do that
> by metamethods.
>

What about using __multiindex and __newmultiindex for the metamethods
for the new cases tbl[i,j] and tbl[i,j]=foo (or tbl[i,j]=foo,bar), and
keep using __index and __newindex for one-argument indices? Then there
will be no performance overhead, and no confusion...

Btw, I believe that the default behavior for both tbl[i,j] and
tbl[i,j]=foo should be just to raise an error - "multi-indexing
behavior not defined".

  Cheers,
    Eduardo Ochs
    eduardoochs@gmail.com
    http://angg.twu.net/
    http://angg.twu.net/ferramentas-para-ativistas.html