Proposal: Automatic Resource Management
Neal Gafter
neal at gafter.com
Tue Mar 3 22:13:32 PST 2009
On Tue, Mar 3, 2009 at 9:53 PM, Bob Lee <crazybob at crazybob.org> wrote:
> I wouldn't mind having closures one day, but based on the current state of
> the BGGA proposal, I think I'd like to have Automatic Resource Management
> either way, preferably sooner rather than later. To help illustrate, can you
> please show us what this snippet would look like using BGGA closures?
>
> try (InputStream in = new FileInputStream(src);
> OutputStream out = new FileOutputStream(dest)) {
> byte[] buf = new byte[8 * 1024];
> int n;
> while ((n = in.read(buf)) >= 0)
> out.write(buf, 0, n);
> } catch (IOException e) {
> showDialog("Copy failed.");
> }
It depends on what libraries are provided, but I'd expect it to be
something like
try {
with (InputStream in : new FileInputStream(src))
with (OutputStream out : new FileOutputStream(dest)) {
byte[] buf = new byte[8 * 1024];
int n;
while ((n = in.read(buf)) >= 0)
out.write(buf, 0, n);
}
} catch (IOException ex) {
showDialog("Copy failed.");
}
Looks about as nice, except for the advantage that the semantics don't
have to be hardcoded into the language. Can you please show me how to
handle the following BGGA example using this proposal:
lockWrite(lock) {
clientCount++;
}
lockRead(lock) {
return field.getValue();
}
More information about the coin-dev
mailing list