lua-users home
lua-l archive

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


> So, why does work the function readonlytable() and why doesn't
> the "inline" form of same method?

They are not the same code. In readonlytable you create a new table
whose metatable's __index points at the passed in table (called
"table"). So there are 3 tables in this story:

a totally empty table, which is returned
that table's metatable
the table in which values are looked up

In the other copy of the code, the first and third tables are the
same, rather than being different. This is bad. You really want to
make sure the first table is totally empty. __newindex and __index are
only called if the key cannot be looked up in the table, so they won't
be called for the entries that are actually in the table.