lua-users home
lua-l archive

Re: event handler for table initialization

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


On Tue, Sep 21, 2010 at 2:47 AM, Nilson <nilson.brazil@gmail.com> wrote:
> 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.
when you want to reorder things in time, the usual answer is to pass
around closures. in your case, the easiest would be to write your
functions as:
local function force_futures (t)
 for k,v in pairs(t) do
 if type(v) == 'function' then
 t[k] = v()
 end
 end
end
function Memory(t)
 return function ()
 -- do some work with 't'
 --......
 force_futures(t)
 return t -- could be a different object
 end
end
function Motherboard(t)
 return function ()
 -- do some work with 't'
 --......
 force_futures(t)
 return t -- could be a different object
 end
end
-- 
Javier

AltStyle によって変換されたページ (->オリジナル) /