[java-gnome-hackers] Potential issues with new closures in JDK 7
Brought to you by:
afcowie
From: Jacek F. <ja...@gm...> - 2009-11-19 15:15:57
|
As you've probably heard by now, looks like JDK 7 will be giving us closures/lambdas after all (as announced yesterday at Devoxx): So, instead of a verbose listener like this in Swing: button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Hi!"); } } ); we could have a much more concise: button.addActionListener(#(ActionEvent e) System.out.println("Hi!)); As you can see, the new closures are designed to be backwards compatible, i.e. able to be used in method that expect an interface with just one method, which makes them usable with existing APIs. I am not sure however if this would work so well with the standard event APIs in Java-GNOME. Let's look at these two examples: * **connect(new Widget.KeyPressEvent() {* * **public boolean onKeyPressEvent(Widget source, EventKey event) {* * **return false;* * **}* * **});* * * * connect(new Widget.KeyReleaseEvent() {* * **public boolean onKeyReleaseEvent(Widget source, EventKey event) {* * **return false;* * **}* * **});* With the new closure syntax both calls would look the same: *connect(#(Widget source, EventKey event) return false);* I would guess there would be no way for the compiler to figure out in this case whether it is a KeyPressEvent or KeyReleaseEvent, making JDK 7 closures potentially unusable with Java-GNOME. Maybe there will be a need for actual event-specific connect methods? Example: *connectKeyPressEvent(#(Widget source, EventKey event) return false);* *connectKeyReleaseEvent(#(Widget source, EventKey event) return false);* * * Jacek |