lua-users home
lua-l archive

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


Am 15.12.10 18:27, schrieb Luiz Henrique de Figueiredo:
>> 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.
> 
> Can't you set an __index for nil that returns nil?
> I think this has been suggested before, perhaps by me :-)
> 
>> debug.setmetatable(nil,{__index=function () return nil end})
>> =a.b
> nil
>> return a.b.c
> nil
> 

fyi, I solved this problem.  I did not put to much magic in my code.  So
instead of writing

	for n = 1, res:ntuples() do
		hdf.bset[n].id = res:getvalue(n, 1)
		hdf.bset[n].name = res:getvalue(n, 2)
		hdf.bset[n].description = res:getvalue(n, 3)
	end

I write

	hdf.bset = {}
	for n = 1, res:ntuples() do
		hdf.bset[n] = {}
		hdf.bset[n].id = res:getvalue(n, 1)
		hdf.bset[n].name = res:getvalue(n, 2)
		hdf.bset[n].description = res:getvalue(n, 3)
	end

Which is more explicit anyway and looks more like "stock" Lua.  So no
magic in table creation and it worked.