United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6433012 A simplified syntax for using Runnable
JDK-6433012 : A simplified syntax for using Runnable

Details
Type:
Enhancement
Submit Date:
2006-06-01
Status:
Closed
Updated Date:
2011-10-31
Project Name:
JDK
Resolved Date:
2011-10-31
Component:
specification
OS:
linux
Sub-Component:
language
CPU:
x86
Priority:
P4
Resolution:
Duplicate
Affected Versions:
6
Fixed Versions:

Related Reports
Duplicate:
Relates:
Relates:
Relates:
Relates:
Relates:

Sub Tasks

Description
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 ----------

                                    

Comments
EVALUATION

To be considered as part of the closure debate.
                                     
2006-11-14



Hardware and Software, Engineered to Work Together