lua-users home
lua-l archive

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

The current 5.4 beta manual states:

The value assigned to a to-be-closed variable must have a __close metamethod or be a false value.


This very likely implies that the __close metamethod must be present at the time when the value is assigned to the variable. If this is indeed the expectation, then it would be good to have it stated more explicitly. The manual does not specify the behavior for the case when the metamethod is absent. It would also be good to have an explicit statement of that.

Then, it may be inferred that when the variable is being closed, the originally seen value of __close will be called, i.e., changes to the value of __close in the metatable will not be effective. If this is what is indeed implied, it would be good to see that stated as such in the manual.

On the other hand, I would like to say that the above seems like an unnecessary complication that could be wholly removed if the manual said instead of the cited sentence (and the behavior matched): 

When a variable is being closed, the current value of the __close metamethod, if any, is used.


This makes it possible to "close" anything, without that having any effect (except in performance perhaps) and change what closing will do during the lifetime of the variable. I think this will also save a few CPU cycles in the ordinary case when closing has an effect.

Note that unlike the __gc case, where there is no natural way for the VM to learn that a value should be marked for finalization, and __gc is checked when a metatable is assigned, <toclose> in the local declaration tells the compiler explicitly that closing is requested, so there is no real need to check for __close at this point.