lua-users home
lua-l archive

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


Jamie Webb wrote:
> Option 2) Of course, you're going to run into problems
> if this function uses any other globals ...
Thank you.
And how I can dispose of return function() in inc?
Is it impossible not use "return function" to create new 
instance?
Maybe somehow local rf = newinstance(fn) :)
(I don't want change chunkreader and every time add header 
and footer to files.)

---
local function inc()
	--?return function()
		out:write(tostring(out), '\n')
	--?end
end

local function make_instance(fn, out)
	local env = setmetatable({ out = out }, { __index = _G 
})
	local rf = fn--?()
	setfenv(rf, env)
	return rf
end

local a = make_instance(inc, io.stdout)
local b = make_instance(inc, io.stderr)