lua-users home
lua-l archive

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


It occurred to me that a nice way to express the hook after a lexical
block might be as follows, somewhat inspired by for loop iterators.

	in env, hook do
		block
	end

where "env" is the new environment table and (if it is present and not
nil) "hook" is the function to be called (with no arguments) when the
block finishes. Then libraries can define functions that return env, hook
pairs, e.g.

	function dynamic(t)
		local save = {}
		for k,v in pairs(t) do
			save[k] = _G[k]
			_G[k] = t[k]
		end
		return _G, function()
			for k,v in pairs(save) do
				_G[k] = v
			end
		end
	end

lhf's neat dynamic environment idea would then have to be written

	f = loadstring("in ..., nil do "..block.." end")

Tony.
-- 
f.anthony.n.finch  <dot@dotat.at>  http://dotat.at/
GERMAN BIGHT HUMBER: SOUTHWEST 5 TO 7. MODERATE OR ROUGH. SQUALLY SHOWERS.
MODERATE OR GOOD.