lua-users home
lua-l archive

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


Hi Robert,

first, many thanks for the clarification,

On Tue, Jun 23, 2020 at 07:20:04PM +0900, Robert Burke wrote:
> > 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.

so if I understand correctly I can make a _real_ read-only table
only with the readonlytable() function? And in this case what happens
with the "original" table? There is a point when two tables
exists in same time in memory? When readonlytable() returns, it
freed? It's interesting from the point of memory usage.

Thanks,


a.