lua-users home
lua-l archive

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


Hi,

The beta release of 5.4 prompted me to finally try something I have
been thinking about.
Instead of the <close> variable and __close metamethod, the approach I
have taken is this:

A new keyword 'defer' introduces a deferred function that will be
executed on block scope exit.
The function cannot be named, hence you cannot reference it in Lua code.
Under the cover the defer statement introduces a 'hidden' local
variable and generates op code to create an upvalue for it. The value
assigned to this must be a function.

I have a very early commit - based on Roberto's initial work on
toclose vars, in my git repository (link below). The implementation is
not complete and not working correctly yet.

https://github.com/dibyendumajumdar/ravi/commit/e8a5f2245c34d00fb6684ce235dbf52d908fa8a2

Example session:

> function x()
>>   defer function() print 'exit x' end
>> end
> x()
exit x
> function x()
>>    defer function() print 'exit x' end
>>    error('err')
>> end
> x()
exit x
stdin:3: err
stack traceback:
    [C]: in function 'error'
    stdin:3: in function 'x'
    (...tail calls...)
    [C]: in ?