lua-users home
lua-l archive

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

The __index approach is clever and a great solution for some cases, but it
doesn't handle everything where you want to copy.

That suffers from the problem that as long as B is around A stays around. If
A references a large structure via an entry hidden by B that structure won't
get collected even though no code will ever get to it.

While deepcopy is a much more complicated problem, I really wish there were
a copy operation with a standard implementation for the various types
(including doing nothing for strings since they are immutable) and metatable
support for overriding it. Yes, one can write this, but it could probably be
written much efficiently in something that was intimate with the
implementation data structures.


on 6/17/03 10:07 AM, Luiz Henrique de Figueiredo at

>> I have a system using templates. The application users will take an
>> existing table, and clone it to create a copy which they then can tweak.
> Instead of cloning a table A into a table B, why not create an empty table B
> and set the __index metamethod of B to be A? With this setup, reading from B
> reads from A, but writing to B writes to B, not to A. To handle tables inside
> A,
> the __index metamethod would have to set the same scheme for these tables.
> --lhf