trying out the prototype (original) (raw)
Joe Darcy joe.darcy at oracle.com
Tue Aug 24 11:55:21 PDT 2010
- Previous message: trying out the prototype
- Next message: trying out the prototype
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Zhong Yu wrote:
How about this:
No.
Trying out the prototype is an invitation to give feedback on the prototype. It is not an invitation to start designing something fundamentally different without giving any detailed suguaring or semantics (or code).
-Joe
// any block { int c;
try FileReader reader = new FileReader( source ); try FileWriter writer = new FileWriter( target ); while( (c = reader.read()) != -1 ) writer.write(c); } The [try ResourceSpecification] statement can appear anywhere in a block. When the enclosing block completes, close() methods are invoked. It's almost as if we have destructors public void copy(File source, File target) throws IOException { int c; try FileReader reader = new FileReader( source ); try FileWriter writer = new FileWriter( target ); while( (c = reader.read()) != -1 ) writer.write(c); } // reader and writer will be closed upon method completion Since we piggyback it on an existing block, we can avoid one indentation, and making code more readable. Compatibility isn't broken, the new behavior of the block only arise if the new type of statement appears inside. The "try" keyword probably sounds unnatural for this purpose. Maybe we can use some other keyword or symbol. IDE should warn when it appears that an auto-closeable resource is not, but should be, initialized this way. Note, the auto-close behavior stays the same even if the enclosing block is a try block try { int c; try FileReader reader = new FileReader( source ); try FileWriter writer = new FileWriter( target ); while( (c = reader.read()) != -1 ) writer.write(c); } catch(IOException e) { // reader and writer already closed! } Zhong Yu
On Tue, Aug 24, 2010 at 8:40 AM, Serge Boulay <serge.boulay at gmail.com> wrote: The "using" block in c# only allows one resource unless the resources are of the same type. To use multiple resources they are nested or stacked using (StreamWriter w1 = File.CreateText("W1")) using (StreamWriter w2 = File.CreateText("W2")) { // code here } instead of using (StreamWriter w1 = File.CreateText("W1")) { using (StreamWriter w2 = File.CreateText("W2")) { // code here } } Depending on the number of resources, the "using" block nesting can quickly get out of hand.
On 8/24/10, Stephen Colebourne <scolebourne at joda.org> wrote: On 24 August 2010 12:14, David Holmes <David.Holmes at oracle.com> wrote: (I guess this forms a case against the try-with-multiple-resources statement in general. The list of semicolon-delimited declarations enclosed by parentheses looks weird, anyway ;-) I tend to agree the syntax is awkward and far less readable than simply nesting the try-with statements. Overall, I think the semicolon, multi-resource, aspect is more complex than the benefits it gives. Clearer code results from nesting the statements. Stephen
- Previous message: trying out the prototype
- Next message: trying out the prototype
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]