Wednesday, July 7, 2010

Java Closures

Java 7 is going to have lambda expression("closure"). Some people say it's simplification.
Other says no it's harm. Well anonymous classes was a replacement of closures and
as James Gossling says because of time pressure. But the Java culture of programming is formed and
exists a decade. Just try yourself the examples and ask youself:
Is it improve you code? Ask colleges do they understand your code?
Example 1

val SOME 6 =
withReturn
(fn return =>
(app (fn x =>
if 0 = x mod 2 then
return (SOME x)
else
print (Int.toString x))
[1, 3, 5, 6, 7, 9]
; NONE))

What's the result??? It's simple isn't it? 135

Example 2

new Functor(){public Object f(Object x){ ... }}
vs
(lambda (x) ...)
vs
new (){f(x){...}}


Example 3

textField.addActionListner( new ActionListener() {
public void actionPerformed( ActionEvent notUsed ) {
textArea.append( textField.getText() );
}
});
vs
textField.addActionListner( ActionListener()
( notUsed )
textArea.append( textField.getText() );
);

Result: As we see from examples we missing type declaration - so missing the idea
of Java as a language. Simplification of lines of code,lines...No the most for you you IDE
does no matter Eclipse, IntelJ or Netbeans. But if you are a vi or notepad geek just
think maybe it's better for you to write on perl or assembler?