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

Type arguments for a class shall be inferred on constructor invocation

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 5.0
    • specification
    • sparc
    • solaris_9

      A DESCRIPTION OF THE REQUEST :
      Consider the following piece of code:

      List<Map<String,List<Integer>>> list = new ArrayList<Map<String,List<Integer>>>();

      One has to repeat type parameters twice (and they can be even longer
      than in this example). This kind of repetition of type arguments is
      bug-prone and creates unnecessary clutter in the code. One shall be
      able to write this code simpler, like this:

      List<Map<String,List<Integer>>> list = new ArrayList();


      JUSTIFICATION :
      For ease of development.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The proposed code shall compile normally via type parameter inference mechanism.

      ACTUAL -
      This code complies, but produces unchecked warnings.


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

      public class ListCreate {
      public static void main(String[] args) {
      List<Map<String,List<Integer>>> list = new ArrayList();
      }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Use static factory methods instead of constructors. They work just
      fine with type argument inference without any warnings.

      import java.util.*;

      public class ListCreate2 {
              public static void main(String[] args) {
                      List<Map<String,List<Integer>>> list = newArrayList();
              }

              public static <T> ArrayList<T> newArrayList() {
                      return new ArrayList<T>();
              }
      }
      ###@###.### 2005-1-24 13:16:39 GMT

            abuckley Alex Buckley
            jssunw Jitender S (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: