Module:Sandbox/Jackmcbarn

This is an old revision of this page, as edited by Jackmcbarn (talk | contribs) at 18:38, 13 May 2014 (thoughts about making duplicate upvalues). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

local function makeCounter()
	local counter = 0
	return function()
		counter = counter + 1
		return counter
	end
end

function p.main(frame)
	local myCounter = makeCounter()
	myCounter()
	myCounter()
	myCounter()
	myCounter()
	myCounter() -- this returns 5
	local newCounter = myCounter
	newCounter() -- this returns 6
	return myCounter() -- this returns 7, since newCounter and myCounter are really the same thing. is there any way to make a copy of a function that doesn't share upvalues?
end

return p