lua-users home
lua-l archive

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


Hi guys,

Using function calls and tables arguments to describe structures like
the example below

Computer{
	brand = 'XYZ';
	Motherboard{
		Processor{type = 'Pentium'};
        Memory{size = '512MB'}
    };
    Harddisk{capacity='120GB'}
};

will generate function calls in an order completely different of reading order.
In the example: Processor, Memory, Motherboard, Harddisk, Computer.

The first and outter function is the last one to be called.
And more important, the outter function only has access to the table
after the initialization is completed.
There is neither an opportunity to the outter function to set the
metatable before initialization nor to set an internal state in
correct order.

Possible solution: a table constructor event handler

The idea is create a mecanism to send events during the table
initialization to the table constructor event handler.

This can be done changing the syntax of a normal function definition
to inform that the function must be called to receive table creation
events.

(draft  draft  draft)
Possible syntax:

local computer_mt = {}

function Computer { tab }  -- note {} instead of ()
	local evt = table.getevent() -- or something like this
	if not evt then
		-- normal processing
	elseif evt.type = 'table-creation' then
		setmetatable(tab,computer_mt)
	elseif evt.type = 'before-creation' then
		if evt.key = 'brand' then
			evt.key = 'Brand'
		elseif evt.autokey then	
			error('A key field is required. field#: '..evt.fieldno);
		end
	-- other events with parameters
	end
end

Of course, if Computer was not called using function{}, the
table.getevent will return nil

Interesting events could be:
table-creation, before-field-creation, after-field-creation

Well, that's it.

-- 
Nilson