lua-users home
lua-l archive

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


On Tue, 10 Feb 2009, Roberto Ierusalimschy wrote:

[snip list of cases where one wants to do resource cleanup]

> In all cases the key point is "on exceptions".

For these kinds of expensive resources the point isn't "on exceptions" 
IMHO, but rather the prompt cleanup in all cases.

> Maybe we could avoid these workarounds by forcing a full garbage collection
> upon errors.

The main reason I'm not a huge fan of this solution is that it doesn't 
deal with the far more common non-exceptional case, leaving it up to 
the user to call finalizers explicitly.  It should be a goal of any 
proposed extension to reduce the total amount of code in a program, or 
at least minimise the extra code.

I'm more in favour of scoped cleanup using "with/do" style constructs, 
or the "scoped" keyword proposal (though I prefer renaming it to 
"with.")  At the usage site there's a minimal amount of code to 
introduce the scope, cleanup happens regardless of exceptions, and 
depending on syntax it may even net a negative amount of code since 
the explicit calls to finalizers in the normal doesn't need to be 
there.  e.g. Replacing the "scoped" keyword with "with" but otherwise 
keeping similar semantics, it might look like this:

do with f, g = io.open("src.txt","r"), io.open("dst.txt",w")
   for l in f:lines() do
      ...
      g:write(l)
   end
end

Cheers,

Joonas