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:22 PM, Peter Cawley <lua@corsix.org> wrote:
> On Tue, Jun 28, 2011 at 9:15 PM, Alexander Gladysh <agladysh@gmail.com> wrote:
>> Note, as comment says, the object can not be table — tables can be
>> changed, and, with Lua dynamic type system, this *will* lead to weird
>> bugs, where unique_object() result is accidentally used instead of a
>> table.
>
> And a table given the metatable of {__newindex = error, __metatable =
> false}, thus making it immutable, isn't acceptable?

Or if you really want to use a function, rely on the fact that
debug.upvalueid gives a detectable difference to things with different
upvalues, allowing you to do the following:

local unique_object = function()
  local function f() return f end
  return f
end