lua-users home
lua-l archive

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


This cannot work. I just tried to code something up that builds a
stack on __index and only creates the objects when the __newindex
arrives. This could theoretically work (with the disadvantage the keys
possibly been left in the stack hindering the GC).

The much graver problem than only creating on write access is that you
want a.b.c.d to nil if not defined. Not some sort of tracking table.
This cannot work, since there is no way to tell in an __index if this
is the last element or not and to return some table that helps
tracking following indexes or if it should return nil already.

On Wed, Dec 15, 2010 at 5:22 PM, Marc Balmer <marc@msys.ch> wrote:
> Am 15.12.10 17:09, schrieb Luiz Henrique de Figueiredo:
>>> Is there a way to detect in an __index meta-method, written in C, if the
>>> access is for reading or writing a value?  I.e. to distinguish between
>>>
>>> a.b.c = 42
>>>
>>> and
>>>
>>> local answer = a.b.c
>>
>> Perhaps you mean this? http://lua-users.org/wiki/AutomagicTables
>
> Similar, yes.  I successfully created it in C to access ClearSilver HDF
> structures, however
>
> a.b.c.d = "a.b and a.b.c are automatically created"
>
> this part works. But when I access a not-yet existing valued, e.g.
>
> local n = a.b.x.y
>
> Then I want to get nil.  Unfortunately what happens, is that a.b and
> a.b.x, a.b.x.y get created.
>
> So write access should create the whole chain, whereas read access
> should not.
>
>
>
>