lua-users home
lua-l archive

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


> On Tue, Jul 24, 2018 at 5:43 PM, John Belmonte <john@neggie.net> wrote:
> > Sorry didn't realize that was psuedocode.  Yes, same thing I proposed in
> > another thread.  But exit func must be a new 4th parameter else you'll break
> > a lot of existing code.
> 
> I don't really see why? IIRC, the proposal was that the __exit
> metamethod is called only if it exists. So if you want your "state"
> object to be released if the loop breaks, add an __exit metamethod to
> new code. Fortunately, it doesn't break anything in old versions of
> Lua; it maintains the status quo.

This is correct.

One problem with this proposal (or the similar one with a 4th parameter)
is that we will have to create the scoped variable in all loops.
Consider this code:

     do
       local f, s, var = explist
       local <mark> s = s -- added!
       while true do
         ...

The creation of a scoped variable needs to allocate memory, and therefore
it could fail without creating the scoped variable!
So, I think the correct code should be more like this:

     do
       local <mark> _s = nil -- added!
       local f, s, var = explist
       _s = s -- added!
       while true do
         ...

Anyway, Lua will have to allocate a scoped variable for every loop,
independently whether it needs it or not.

-- Roberto