[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Getting a reference to the current function
- From: David Given <dg@...>
- Date: Wed, 04 Oct 2006 20:01:52 +0000
Gavin Kistner wrote:
[...]
> Does anyone have any suggestions on how to pre-allocate a few userdata
> objects and associate them with a specific function, so that when that
> function is called (from different 'scopes' as the self using Lua's OOP
> syntax sugar) you can access them?
You can use closures for this.
local function f_maker()
local foo = 1
local bar = 2
local baz = 3
return function(a, b, c)
foo = foo + a
bar = bar + b
baz = baz + c
return foo + bar + baz
end
end
f = f_maker()
f is now a global function with three items of static data (in C parlance).
Does that help?
--
David Given
dg@cowlark.com