lua-users home
lua-l archive

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

I have thought more about my original proposal, which was to introduce a new keyword that makes a variable subject to deterministic finalization.

The chief difficulty is that an object referenced by such a variable can be assigned to another variable (stored in a table, captured in a closure), and vice versa. And, if that is not prevented, then the other variable may end up holding a reference to a finalized object. Today's resurrection could also result in that, but today resurrection is fully controlled by the finalizer and hence its library's writer; changing that will make a lot of existing libraries invalid.

At this point it is tempting to say "let's make it impossible to assign block-scope vars to other vars, etc"; after a minute's reflection it is clear that such block-scope variables would be useless. So that approach won't work. We should not finalize anything while it is still being referenced.

As far as I can tell, there is only one well-known method for deterministic finalization: reference counting. It is also well known that it does not work when references can be circular. However, a combination of reference counting AND garbage collection will ensure that finalization WILL happen, and will happen deterministically if a certain coding paradigm is followed, such as only storing reference-counted objects in local vars. The deterministic condition can probably be relaxed to cover a larger set of use cases, but local-vars-only would already be pretty good.

I realize that ref counting will introduce certain overhead; but before we even go there, is there any fundamental reason why ref counting + GC cannot work in Lua? Note that such hybrid solutions exist in other languages, such as Python [1].