lua-users home
lua-l archive

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


On Mon, Aug 1, 2016 at 4:29 AM, Soni L. <fakedme@gmail.com> wrote:
If I have this:

local t = {}
local another_function(wrapped, ...)
  print(t[wrapper])
end
local function wrap(something_important)
  local function wrapper(...)
    return another_function(wrapper, ...)
  end
  t[wrapper] = something_important
  return wrapper
end

Is it guaranteed that 2 calls to wrap() won't cause weird fucked-up semantics? E.g. can I do this?

local wrap1 = wrap(1)
local wrap2 = wrap(2)
wrap1() --> prints 1
wrap2() --> prints 2

Yes, closure "wrapper" is guaranteed to be a new function every time.
Because this closure refers to local variable "wrapper" which is different every time.