-
Enhancement
-
Resolution: Duplicate
-
P4
-
None
-
6
-
x86
-
linux
A DESCRIPTION OF THE REQUEST :
Simplify creating a Runnable by using the 'do' keyword in a new way that is nevertheless very Javalike. Example:
Runnable r = do(foo, bar) { System.out.println(foo + bar + baz); }
The compiler generates this
Runnable r = new $$$R1(foo, bar);
and this to go with it:
private class $$$R1 implements Runnable {
$$$R1(String foo, int bar) {
this.foo = foo;
this.bar = bar;
}
private final String foo;
private final int bar;
public void run() {
System.out.println(foo + bar + baz);
}
}
The same syntax could be used for Callable. The compiler decides whether to create a Runnable or a Callable simply by the presence of a return statement with an argument.
Now why was it again that Callable is not in java.lang?
JUSTIFICATION :
This proposal eliminates a lot of clutter. When you need a Runnable that has a constructor with arguments, it eliminates a huge amount of clutter. Many proposals involving new language syntax are nonstarters because of the problem of keyword use. In this case, I think it is fortunate that 'do' is a perfect fit.
---------- BEGIN SOURCE ----------
import java.util.concurrent.Callable;
class EasyRunnable {
public static void main(String[] args) throws Exception {
// new EasyRunnable().proposal();
new EasyRunnable().worksLikeThis();
}
String baz = " so far.";
static void proposal() throws Exception {
String foo = "Objections: ";
int bar = 0;
// Runnable r = do(foo, bar) { System.out.println(foo + bar + baz); }
// r.run();
// Callable<String> c = do(foo, bar) { return foo + bar + baz; }
// System.out.println(c.call());
}
void worksLikeThis() throws Exception {
String foo = "Objections: ";
int bar = 0;
Runnable r = new $$$R1(foo, bar);
r.run();
Callable c = new $$$C1(foo, bar);
System.out.println(c.call());
}
private class $$$R1 implements Runnable {
$$$R1(String foo, int bar) {
this.foo = foo;
this.bar = bar;
}
private final String foo;
private final int bar;
public void run() {
System.out.println(foo + bar + baz);
}
}
private class $$$C1 implements Callable<String> {
$$$C1(String foo, int bar) {
this.foo = foo;
this.bar = bar;
}
private final String foo;
private final int bar;
public String call() {
return foo + bar + baz;
}
}
}
/**
* Created by IntelliJ IDEA.
* User: ###@###.###
* Date: Jun 1, 2006
* Time: 10:59:32 AM
*/
---------- END SOURCE ----------
Simplify creating a Runnable by using the 'do' keyword in a new way that is nevertheless very Javalike. Example:
Runnable r = do(foo, bar) { System.out.println(foo + bar + baz); }
The compiler generates this
Runnable r = new $$$R1(foo, bar);
and this to go with it:
private class $$$R1 implements Runnable {
$$$R1(String foo, int bar) {
this.foo = foo;
this.bar = bar;
}
private final String foo;
private final int bar;
public void run() {
System.out.println(foo + bar + baz);
}
}
The same syntax could be used for Callable. The compiler decides whether to create a Runnable or a Callable simply by the presence of a return statement with an argument.
Now why was it again that Callable is not in java.lang?
JUSTIFICATION :
This proposal eliminates a lot of clutter. When you need a Runnable that has a constructor with arguments, it eliminates a huge amount of clutter. Many proposals involving new language syntax are nonstarters because of the problem of keyword use. In this case, I think it is fortunate that 'do' is a perfect fit.
---------- BEGIN SOURCE ----------
import java.util.concurrent.Callable;
class EasyRunnable {
public static void main(String[] args) throws Exception {
// new EasyRunnable().proposal();
new EasyRunnable().worksLikeThis();
}
String baz = " so far.";
static void proposal() throws Exception {
String foo = "Objections: ";
int bar = 0;
// Runnable r = do(foo, bar) { System.out.println(foo + bar + baz); }
// r.run();
// Callable<String> c = do(foo, bar) { return foo + bar + baz; }
// System.out.println(c.call());
}
void worksLikeThis() throws Exception {
String foo = "Objections: ";
int bar = 0;
Runnable r = new $$$R1(foo, bar);
r.run();
Callable c = new $$$C1(foo, bar);
System.out.println(c.call());
}
private class $$$R1 implements Runnable {
$$$R1(String foo, int bar) {
this.foo = foo;
this.bar = bar;
}
private final String foo;
private final int bar;
public void run() {
System.out.println(foo + bar + baz);
}
}
private class $$$C1 implements Callable<String> {
$$$C1(String foo, int bar) {
this.foo = foo;
this.bar = bar;
}
private final String foo;
private final int bar;
public String call() {
return foo + bar + baz;
}
}
}
/**
* Created by IntelliJ IDEA.
* User: ###@###.###
* Date: Jun 1, 2006
* Time: 10:59:32 AM
*/
---------- END SOURCE ----------
- duplicates
-
JDK-5014235 Closures support instead of anonymous inner classes
-
- Closed
-
- relates to
-
JDK-4300222 RFE: Object Oriented Callbacks
-
- Closed
-
-
JDK-6315058 Anonymous functions
-
- Closed
-
-
JDK-6463976 Add Lambda, Thunk, Predicate, etc., to complement Runnable
-
- Closed
-
-
JDK-6487635 Compiler support for methods as method parameters
-
- Closed
-
-
JDK-6500704 Add language support for converting methods to interfaces
-
- Closed
-
(1 relates to)