 |
Garden thought
Posted by forax on September 02, 2006 at 07:44 AM | Comments (8)
I'm seating in my garden, my laptop on my knees.
i'm hearing the noise of some mowers in the background,
my kid is sleeping,
my wife is trying to resolve a sudoku,
and i'm thinking about closure, again !
Why doesn't enable to define a closure using a reference
to a method ?
public class HelloClosure {
public void sayHello(String name) {
System.out.println("hello "+name);
}
public static void main(String[] args) {
HelloClosure hello=new HelloClosure();
void(String) sayHello=hello.sayHello;
sayHello("closure");
}
}
The syntax seems obvious, the code (hello.sayHello)("closure")
is semantically equivalent to hello.sayHello("closure").
The compiler has only to verify that there is no field named identically
in the scope before interprets it as a closure.
It's not very difficult to translate, the type of the closure is
the type of the method, the referer of a non static method will be store
in a field of the closure like any other local variable.
public static void main(String[] args) {
HelloClosure hello=new HelloClosure();
void(String) sayHello=void(String s) {
hello.sayHello(s);
};
sayHello("closure");
}
And by using the closure convertion, register an Actionlistener
on a button could become as simple as that:
public class Application {
public void onButtonAction(ActionEvent event) {
System.out.println("damn, i'm clicked");
}
public static void main(String[] args) {
Application application=new Application();
JButton button=new JButton("Ok !");
button.addActionListener(application.onButtonAction);
...
}
}
What do you think ?
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
As I understand it, the proposal already allows something like:
public class HelloClosure {
private final void(String) sayHello = (String name) {
System.out.println("hello "+name);
};
public static void main(String[] args) {
HelloClosure hello=new HelloClosure();
void(String) sayHello=hello.sayHello;
sayHello("closure");
}
}
I don't think there's a great deal of point complicating things any further (other than to remove that bloody semicolon).
Posted by: tackline on September 02, 2006 at 08:39 AM
-
no... more... closures...
Posted by: kirillcool on September 02, 2006 at 10:00 AM
-
Sounds scary, what if there is field named sayHello of type void(String). Seems like a source of bugs.
Maybe some special keyword is order here, like what they have in c#
Posted by: sidharthkuruvila on September 02, 2006 at 01:03 PM
-
@tackline, one interrest of my proposal is that methods are overiddable
unlike local functions or fields. The idea is to provide some sort of unification: function type can reference a local function or a method and can be converted in anonymous class.
@kirillcool, i agree, tomorrow i give up, do you know a good patch :)
@sidharthkuruvila, javac must check fields first in order to be backward compatible, but as you said a keyword is better.
Posted by: forax on September 02, 2006 at 02:30 PM
-
But then,how do you remove the action listener?
button.removeActionListener( application.onButtonAction )
won't work, because a *new* closure will be created by this call, which will not match the previously added closure.
Posted by: vieiro on September 04, 2006 at 11:37 PM
-
Yes, it's the more general problem of closure interning,
see my answer to pago in that forum :
http://forums.java.net/jive/thread.jspa?threadID=17849&start=30&tstart=0
it's technically possible to reuse the same object and
i hope the closure specification will be clear about that.
Rémi Forax
Posted by: forax on September 04, 2006 at 11:52 PM
-
"The syntax seems obvious, the code (hello.sayHello)("closure") is semantically equivalent to hello.sayHello("closure"). "
In fact, it's so similar there's no need for it at all...
Reeks like change for the sake of change to me, like most things screamed about as "requirements to make Java survive" etc. etc.
Posted by: jwenting on September 05, 2006 at 03:50 AM
-
Your suggested syntax is great for mapping reference.method to an instance of an interface with one method. Many listeners have only one method but what abot more complicated listeners like all those interfaces that have Adapters?
Designing new language syntax is hard. Harder still if trying to retrofit it.
Posted by: arae on September 05, 2006 at 08:41 AM
|