lua-users home
lua-l archive

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


On Tue, Jun 28, 2011 at 9:38 PM, Alexander Gladysh <agladysh@gmail.com> wrote:
> Or am I wrong? Can one say that manual wording guarantees that this
> will always work in "conforming" implementation?

The wording in debug.upvalueid effectively says that the IDs of two
upvalues will be equal if and only if the upvalues point to the same
external local. I'd then consider calling debug.upvalueid to be
included in the "any detectable difference" of 3.4.3. You could
possibly argue that in my prior example, distinct instances of f could
validly share an upvalue, so perhaps you'd be slightly happier if the
closures being created certainly couldn't share an external local:

local unique_object = function()
  local slot
  local function f(...)
    local old = slot
    slot = ...
    return old
  end
  return f
end

An alternative might be to construct closures which have differing
behaviour upon being called:

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