lua-users home
lua-l archive

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


On Wed, Jun 29, 2011 at 00:56, Peter Cawley <lua@corsix.org> wrote:
> 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

Uh. Kind of scary. :-)

Alexander.