Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6433012

A simplified syntax for using Runnable

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 6
    • specification
    • 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 ----------

            abuckley Alex Buckley
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: