lua-users home
lua-l archive

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


On 13 January 2011 05:49, Tang Daogang <daogangtang@gmail.com> wrote:
> But a new problem comes - if the table 'a' has already a metatable which is
> not 'table', how to reach this shortcut format?
>

If the metatable it has doesn't have an __index then:

getmetatable(t).__index = table

If it has an __index and it's a function then that needs to return
table[k]. Otherwise if it's a table (sorry for the early-morning
brain-hurt):

setmetatable(getmetatable(t).__index, { __index = table })  ;--)

But seriously, this is why tables don't have a metatable by default -
in case you need to store string keys that are the same as the method
names in table.*. So if you're likely to have keys that conflict with
these methods then make your table store its data in a another table
inside itself instead and have methods for getting/setting.

I feel like the above paragraph could probably be condensed into a
single comprehensible sentence, but it's nearly 9AM and I haven't
slept...

Regards,
Matthew