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

Request for revision of the grammar for try with resources

XMLWordPrintable

    • x86
    • linux

      A DESCRIPTION OF THE REQUEST :
      The current grammar for try-with-resources can be improved. If multiple resources of the same type are to be opened, the grammar is inefficient.

      JUSTIFICATION :
      In other situations, like for (;;) or with "normal" variable declarations, multiple declarations can be performed in one statement.
      It is unexpected for a language user that in the situation of try-with-resources this is not possible.
      Also, I cannot see a justification from a language consistency point of view not to do it.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The following two statements are equal:

      try (
              final InputStream i1 = new FileInputStream(args[0]);
              final InputStream i2 = new FileInputStream(args[1])) {
      }

      try (
              final InputStream i1 = new FileInputStream(args[0]), i2 = new FileInputStream(args[1])) {
      }
      ACTUAL -
      The statement
      try (
              final InputStream i1 = new FileInputStream(args[0]), i2 = new FileInputStream(args[1])) {
      }
      does not compile.

      ---------- BEGIN SOURCE ----------
      import java.io.*;

      /** Demo program for multiple allocations in try with resources.
       * @author <a href="mailto:###@###.###">Christian Hujer</a>
       */
      public class MultipleTest {

          /** Main program.
           * @param args Command line arguments - two filenames, try non-existing file as second argument.
           * @throws IOException in case of I/O problems.
           */
          public static void main(final String... args) throws IOException {
              try (final InputStream i1 = new FileInputStream(args[0]), i2 = new FileInputStream(args[1])) {
              }
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      import java.io.*;

      /** Demo program for multiple allocations in try with resources.
       * @author <a href="mailto:###@###.###">Christian Hujer</a>
       */
      public class MultipleTest {

          /** Main program.
           * @param args Command line arguments - two filenames, try non-existing file as second argument.
           * @throws IOException in case of I/O problems.
           */
          public static void main(final String... args) throws IOException {
              try (final InputStream i1 = new FileInputStream(args[0]); final InputStream i2 = new FileInputStream(args[1])) {
              }
          }
      }

            darcy Joe Darcy
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: