www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Small Changes for Java JDK7

reply bearophile <bearophileHUGS lycos.com> writes:
From:
http://jeremymanson.blogspot.com/2009/02/small-language-changes-for-jdk7.html

Automated Resource Blocks, to be able to say things like:

try (BufferedReader br = new BufferedReader(new FileReader(path)) {
  return br.readLine();
}

instead of:

BufferedReader br = new BufferedReader(new FileReader(path));
try {
   return br.readLine();
} finally {
   br.close();
}

based on having BufferedReader implement a Disposable interface.

------------------

Exception handling improvements:

try {
    doWork(file);
} catch (final IOException | SQLException ex) {
    logger.log(ex);
    throw ex;
}

Bye,
bearophile
Mar 01 2009
next sibling parent reply davidl <davidl 126.com> writes:
在 Mon, 02 Mar 2009 08:02:35 +0800,bearophile <bearophileHUGS lycos.com>  
写道:

 From:
 http://jeremymanson.blogspot.com/2009/02/small-language-changes-for-jdk7.html

 Automated Resource Blocks, to be able to say things like:

 try (BufferedReader br = new BufferedReader(new FileReader(path)) {
   return br.readLine();
 }

 instead of:

 BufferedReader br = new BufferedReader(new FileReader(path));
 try {
    return br.readLine();
 } finally {
    br.close();
 }

 based on having BufferedReader implement a Disposable interface.

 ------------------

 Exception handling improvements:

 try {
     doWork(file);
 } catch (final IOException | SQLException ex) {
     logger.log(ex);
     throw ex;
 }

 Bye,
 bearophile
I believe it is worse than scope(exit) resource.dispose(); in terms of syntax.
Mar 02 2009
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
davidl wrote:
 在 Mon, 02 Mar 2009 08:02:35 +0800,bearophile 
 <bearophileHUGS lycos.com> 写道:
 
 From:
 http://jeremymanson.blogspot.com/2009/02/small-language-changes-for-jdk7.html 


 Automated Resource Blocks, to be able to say things like:

 try (BufferedReader br = new BufferedReader(new FileReader(path)) {
   return br.readLine();
 }

 instead of:

 BufferedReader br = new BufferedReader(new FileReader(path));
 try {
    return br.readLine();
 } finally {
    br.close();
 }

 based on having BufferedReader implement a Disposable interface.

 ------------------

 Exception handling improvements:

 try {
     doWork(file);
 } catch (final IOException | SQLException ex) {
     logger.log(ex);
     throw ex;
 }

 Bye,
 bearophile
I believe it is worse than scope(exit) resource.dispose(); in terms of syntax.
I agree. They keep on missing the point that the blessed code on the normal path MUST NOT TAKE THE HIT OF AN EXTRA INDENTATION LEVEL. I am screaming because people keep on falling in that trap in various languages, mostly by copying without thinking from other languages. The first language I know that did that mistake was Lisp itself. Andrei
Mar 02 2009
prev sibling parent reply Walter Bright <newshound1 digitalmars.com> writes:
bearophile wrote:
 From:
 http://jeremymanson.blogspot.com/2009/02/small-language-changes-for-jdk7.html
 
 Automated Resource Blocks, to be able to say things like:
Reinventing scope, rather badly.
Mar 02 2009
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Walter Bright:
 Reinventing scope, rather badly.
Python has recently introduced something similar to that Java solution: http://www.python.org/dev/peps/pep-0343/ Bye, bearophile
Mar 02 2009
parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Mon, Mar 2, 2009 at 3:57 PM, bearophile <bearophileHUGS lycos.com> wrote:
 Walter Bright:
 Reinventing scope, rather badly.
Python has recently introduced something similar to that Java solution: http://www.python.org/dev/peps/pep-0343/
Does that change the fact that it's a poor imitation of scope?
Mar 02 2009
prev sibling next sibling parent Lutger <lutger.blijdestijn gmail.com> writes:
Walter Bright wrote:

 bearophile wrote:
 From:
 http://jeremymanson.blogspot.com/2009/02/small-language-changes-for-
jdk7.html
 
 Automated Resource Blocks, to be able to say things like:
Reinventing scope, rather badly.
Mar 02 2009
prev sibling parent The Anh Tran <trtheanh gmail.com> writes:
http://www.boost.org/doc/libs/1_38_0/libs/scope_exit/doc/html/scope_exit/alternatives.html

try
{
     File passwd("/etc/passwd");
     BOOST_SCOPE_EXIT( (&passwd) )
     {
         passwd.close();
     } BOOST_SCOPE_EXIT_END
     // ...
}
catch(...)
{
     log("could not get user info");
     throw;
}
Mar 02 2009