[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: 5.2: equality for closures
- From: Alexander Gladysh <agladysh@...>
- Date: Fri, 1 Jul 2011 21:04:44 +0400
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.