lua-users home
lua-l archive

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


On 05/31/2017 06:11 PM, Andrew Starks wrote:
> A closure is a function, plus its environment. This includes its
> upvalues and its global environment, which in Lua 5.2 is an upvalue
> called _ENV.
> 
> An upvalue is a local that has been declared in a parent scope and not
> covered over by a local declaration:
> 
> local o = function()
> local x = 1
> return function ()
> print (x)
> x = x + 1
> end
> end

Function parameters are upvalues too. This looked quite strange to me.

local f =
  function(a)
    return
      function()
        a = a + 1
        return a
      end
  end
local counter = f(0)
print(counter(), counter())

-- Martin