lua-users home
lua-l archive

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


On Fri, 17 Jul 2020 at 18:43, Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
>
> > I am trying to understand this test case in 'locals.lua'
> >
> >   local x = 0
> >   local y = 0
> >   co = coroutine.wrap(function ()
> >     local xx <close> = func2close(function () y = y + 1; error("YYY") end)
> >     local xv <close> = func2close(function () x = x + 1; error("XXX") end)
> >       coroutine.yield(100)
> >       return 200
> >   end)
> >   assert(co() == 100); assert(x == 0)
> >   local st, msg = pcall(co)
> >   assert(x == 2 and y == 1)   -- first close is called twice
> >
> > My question is this:
> >
> > Why is the 'first close is called twice'?
>
> Because it raises an error. When closing a variable normally
> (that is, not handling an error), the call to the closing method
> may cause a stack overflow, and then the closing method will
> not be executed. If that happens, Lua will call the closing method
> again after the error (which opened space in the stack).

In this case the method was called, one of them is called twice
although both raised errors.

>
> (If you follow our recommendation that closing methods should be
> backed-up by finalizers, they already should be idempotent.)
>

How would you define idempotence for a function that has failed by
raising an error?

Regards