lua-users home
lua-l archive

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


Hi Sergey,

On Thu, Jun 6, 2019 at 9:10 AM Sergey Zakharchenko <doublef.mobile@gmail.com> wrote:

I 'like' how you reduce my __close-related question to __gc ones. Unfortunately you're missing the point. __close is not a finalizer. Why you keep insisting I shouldn't rely on the precise timing of calling __close is beyond my understanding. The thing is, I want it (obviously) to run on a non-garbage-collected object. Just like any regular code.

__close works pretty much like the "using (var x = new YourType) { ... }" construct in C#. The object must implement an interface, and that interface gets called at the end of the block or when handling an error/exception that exits the block. The behavior is completely deterministic, and the order of events does not depend on the garbage collector. It's the best new feature in 5.4 IMHO.

If the object implements both __close and __gc then __close is called first, unless you do things like using "local <toclose>" or changing the metatable inside of __gc.