Proposal: Automatic Resource Management (original) (raw)
Neal Gafter neal at gafter.com
Tue Mar 3 23:23:12 PST 2009
- Previous message: Proposal: Automatic Resource Management
- Next message: Proposal: Automatic Resource Management
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Tue, Mar 3, 2009 at 11:00 PM, Joshua Bloch <jjb at google.com> wrote:
Neal,
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, No it doesn't. Not even close. In java, the colon in this position would be read by the typical programmer as "in" (e.g., "for each String s in stringList").
Java programmers already know that ":" doesn't mean "in" except after "for"; they do after all somehow manage to understand labels and assert statements. I think it's a stretch to suggest that anyone would be tempted to read it that way except to make this argument.
Java programmers tend to see a close paren followed by a semicolon at the end of a line as the end of a statement, but as you can see from Bob's example, they're likely to be confused by this ARM proposal.
What we want (and what my proposal provides) is the equals sign (=), which means assignment.
Clearly, there is far more than a simple assignment going on in this code, or there would be little point in suggesting a new language feature. Therefore relying on the intuition of assignment is also misleading.
Also you have an extra level of nesting for each variable.
Just as one does not add a level of indentation for each successive element in a chain of if-then-else statements, one does not add a level of indentation for each resource being managed. You can certainly elect to use an additional block and an additional level of nesting for each variable if you feel it makes the code more clear, and I know you've been preaching that BGGA control invocations should be written that way, but I find the way I wrote it more clear.
- Previous message: Proposal: Automatic Resource Management
- Next message: Proposal: Automatic Resource Management
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]