Wednesday, May 30, 2007

TS-2294 Closures for the Java Programming Language

2007 May 8, 16:40
Tuesday

Neal Gafter, Google

http://developers.sun.com/learning/javaoneonline/j1sessn.jsp?sessn=TS-2294&yr=2007&track=5
http://www.javac.info/
http://gafter.blogspot.com/

A clousure is a "a function that refers to free variables in its lexical context." A free variable is a variable outside of the scope of the function that the function can reference by value (usually???). The variables are bound at the time the closure is made. Closures are for abstraction and program readability.

Gafter stresses that any implementation of closures must not interfere with the language as it stands and, less obviously, with the standard APIs as they are.

Gafter gives interesting looping examples. The loop is bound with a list and a function that is applied to each element of the list. This can be done currently with anonymous classes, but Gafter gives several instances where inner classes fail. Eg, "return" would mean a return from the closure, however, with the anonymous class approximation, "return" returns from the implemented or overriden method.

forEach(strings, new OneArg()
public void invoke(String s) {
if (…) return computeResult(s); // error!
}
});

Areas might serve programmers well:
  1. Fork-join concurrency
  2. Functional and higher-order utilities
  3. Aspect Oriented Programming
  4. Continuations
  5. Multiple dispatch

No comments: