Assign and Deassign meta functions

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Assign and Deassign meta functions

Chris Smith
Hi all,

I mentioned this in another thread but the idea has been festering in my mind over the last few days.  The idea is simply that an object’s metatable can have _assign and _deassign methods which are invoked when that object is (de)assigned to/from a variable, for example:

do
 f = io.open(‘a’) — __assign called on result of io.open(‘a’)
 local x = f — f.__assign() called
 local y = x — x.__assign() called

 y = ’something else’ — y.__deassign() followed by (’something else’).__assign()
end — implicit x.__deassign() and y.__deassign() because they are going out of scope.

It seems to me that this would be a much more general way to implement <toclose> and other similar mechanisms and I’d really like to know if it’s been considered before, and if so what the arguments for and against were.

Regards,
Chris

Chris Smith <[hidden email]>