lua-users home
lua-l archive

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


Hi, sorry I'm late for the party but there's something about the
scope-exit hook solution (the __exit metametod) that I don't get.
Aren't the variables (i.e bindings), and not the values (i.e. the
acquired resources) that get out of scope? Shouldn't finalization
occur whenever the last reference gets tossed? This would require a
ref-counting mechanism (eg. like perl), or am I dreaming again?

Anyway, I think there are two distinct patterns here that need
distinct approaches: one is RAII, and the other is "catch any errors
and do something about it". The second is well addressed by pcall() or
whatever try/catch scheme, but the first is not. IMO, to well address
RAII in a hi-level garbage collected language, we should free the
caller of the responsibility of freeing resources it has acquired. In
such language, there should be no difference (from the RAII pov)
between acquiring memory for a string and opening a file. That's why
no try/catch scheme (nor pcall(), which is equivalent in this respect)
is enough to solve the finalization pattern. So it's the gc that
should do it, which brings the problem down to gc's non-determinism.
So my vote goes for a __immediate_gc metamethod implemented via
ref-counting for determinism. And allow me to hook it from lua! ..do I
hear an "over my dead body" ? :)