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

add varargs array factory methods

    Details

    • Type: Enhancement
    • Status: Open
    • Priority: P5
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description

      The language provides a convenient syntax for creating arrays, but only as a variable initializer. For example, one can write:

          int[] intArray = { 1, 2, 3 };

      instead of

          int[] intArray = new int[] { 1, 2, 3 };

      However, it is not possible to use this syntax in other locations, such as in a method argument list or
      on the right-hand side of an assignment. For example, consider a method that takes an array:

          void foo(int[] intArray) { ... }

      One cannot write

          foo({ 1, 2, 3 });

      and instead one must write

          foo(new int[] { 1, 2, 3 })

      This clutters up the argument list.

      The proposal is to consider adding a set of varargs "array factory" methods, probably to the java.util.Arrays class:

          static int[] of(int... array) { return array; }
          static long[] of(long... array) { return array; }
          static double[] of(double... array) { return array; }
          @SafeVarargs static <T> T[] of(T... array) { return array; }

      This would allow more convenient construction of arrays. For example, the above could be written as

          foo(Arrays.of(1, 2, 3))

      or even

         foo(of(1, 2, 3))

      if static imports are used.

      The element type of the reference array can be controlled with a type witness:

          CharSequence[] csa;
          ...
          csa = Arrays.<CharSequence>of("a", "b", "c");

      However, this is no shorter than just constructing a new array.

      The exact set of primitive overloads to be added is to be determined.

      It needs to be verified that the generic of() method is indeed @SafeVarargs.

      The method call Array.of(1, 2, 3) makes perfect sense, but if we expect static import
      to be used more frequently, a bare of(1, 2, 3) call looks odd. A different name could be
      used, for example, array(1, 2, 3).

        Attachments

          Activity

            People

            • Assignee:
              smarks Stuart Marks
              Reporter:
              smarks Stuart Marks
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: