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

RFE: syntactic sugar to simulate C++ stack-allocted object destructor semantics

XMLWordPrintable

    • x86
    • linux



      Name: jl125535 Date: 07/10/2003


      DESCRIPTION OF THE REQUEST :
      Here's a thought for a feature to add to the language. In
      C++, it is often convenient to attach a clean-up action to
      the destructor of a class so that when a stack-allocated
      instance of that class goes out of scope, the clean-up
      action is executed automatically.

      Typical uses are for File open/close, Lock acquire/release,
      Log enter/exit, Variable increment/decrement, and in C++,
      malloc/free.

      However in Java, objects never have so-called 'automatic'
      lifetime, and nor do they have (synchronous) destructors;
      but Java has the somewhat similar try { .. } finally
      mechanism, and has the special case 'synchronized(e) {..}'
      for locks.

      Therefore I propose the following small, compatible (except
      for new keyword 'auto') addition to the compiler and
      library, which generalises the 'synchronized' statement for
      arbitrary interfaces. The syntactic benefit is I hope,
      obvious, by analogy with the synchronized statement.

      // this goes in java.lang
      interface Automatic {
          void start();
          void stop();
      }

      // example implementation class
      class Trace implements Automatic {
          public void start() { System.out.println("start"); }
          public void stop() { System.out.println("stop"); }
      }

      // example use:
      class s
      {
          // you write:
          public static void main(String[] args) {
              Trace tr = new Trace();
              auto(tr) { // argument must implement Automatic
                  if(args.length == 0) return;
                  System.out.println("here");
              }
          }
          // the compiler desugars this to:
          public static void main(String[] args) {
              Trace tr = new Trace();
              Automatic auto$0 = tr;
              try {
                  auto$0.start();
                  if(args.length == 0) return;
                  System.out.println("here");
              }
              finally {
                  auto$0.stop();
              }
          }
      }


      So the additional syntax as shown here is a statement of the
      form:

          auto(EXPR) BLOCK

      In addition it would be nice to support the form:

          auto(EXPR);

      also, which would implicitly use the remainder of the block
      as the BLOCK argument. This permits even more localised
      code changes. e.g.

      void f() {
          auto(new Trace());
          /* this is the block */
      }

      (The most obvious objection I can see is the 'kitchen sink'
      argument).

      (Review ID: 181875)
      ======================================================================

            darcy Joe Darcy
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: