lua-users home
lua-l archive

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




On Wed, 29 Jun 2011, Alexander Gladysh wrote:

In our code we heavily use this function:

local unique_object = function()
   -- Note that table is not acceptable, since its contents are not constant.
   return function() end
end

It returns globally unique constant object that then (usually) is used
as a table key (usually to avoid name clashes).

You could use:

local function unique_object()
  local v = 0
  return function () v = v + 1; return v end
end

Each closure returned has a reference to a different 'v', and that is 'discernable' if you call the function, so each call to unique_object returns a different closure.

Ge'