lua-users home
lua-l archive

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



On Oct 1, 2009, at 8:06 PM, Jorge wrote:

So, THAT is what closures are for! :)

Among other things, e.g.:

local function counter()
  local aCount = 0

  return function()
   aCount = aCount + 1

   return aCount
  end
end

local aCounter = counter()
local anotherCounter = counter()

print( 'aCounter', aCounter() )
print( 'aCounter', aCounter() )

print( 'anotherCounter', anotherCounter() )

> aCounter	1
> aCounter	2
> anotherCounter	1