Proposal: Automatic Resource Management
Bob Lee
crazybob at crazybob.org
Tue Mar 3 22:44:36 PST 2009
On Tue, Mar 3, 2009 at 10:13 PM, Neal Gafter <neal at gafter.com> wrote:
> 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.
FWIW, the lack of {} after the first with() cause me to take a double take.
The BGGA version is verbose and user-unfriendly enough that I'd still want
Automatic Resource Management. Closing I/O resources properly is one of the
biggest challenges for Java programmers; it deserves a first class solution.
Can you please show me how to
> handle the following BGGA example using this proposal:
>
> lockWrite(lock) {
> clientCount++;
> }
> lockRead(lock) {
> return field.getValue();
> }
You can't, nor does the proposal attempt to address this use case. I'm not
personally interested in addressing that use case at this time because it's
easy enough to write:
writeLock.lock();
clientCount++;
writeLock.unlock();
readLock.lock();
try {
return field.getValue();
} finally {
readLock.unlock();
}
and save yourself two allocations and an implicit layer of indirection.
Bob
More information about the coin-dev
mailing list