The Source for Java Technology Collaboration
User: Password:



Start New Message Post a Reply

Subject:  Closures have nothing to do with dynamic languages
Date:  2007-08-13 13:03:12
From:  jhannes
Response to: No closures!


Closures have nothing to do with dynamic languages, and everything to do with simpler, cleaner, safer code.

Wouldn't you rather write this:
read(new FileReader("foo.txt")) {
|Reader reader|
reader.read(); // etc
}


Than:
FileReader reader = null;
try {
reader = new FileReader("foo.txt");
reader.read(); // etc
} finally {
// Verbose, tricky and error prone
try {
if (r != null) {
r.close();
}
} catch (IOException ignore) {}
}


Or even:

read(new FileReader("foo.txt"), 
// Verbose and hard to understand
new FileProcess() {
public void doWithReader(Reader reader) {
reader.read(); // etc
}
} );


I don't know about you, but this is code that I write many times per day.

 Feed java.net RSS Feeds