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

Iterable<T> should return Iterator<? extends T> rather than Iterator<T>

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P5 P5
    • None
    • 6
    • core-libs
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      When a class implements the Iterable<T> interface through other sub-interfaces, it may be that the class actually inherits multiple Iterable<T> signatures. The class might satisfy all of these signatures, but each Iterable has a different T which is strongly typed. The net result is that it is impossible to implement more than one kind of Iterable even when the type being iterated is a subtype of all the iterables which might be implemented.

      JUSTIFICATION :
      It is required for some cases of multiple interface inheritance.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Multiple interface inheritance should compile when all interface restrictions are met.
      ACTUAL -
      The code does not compile

      ---------- BEGIN SOURCE ----------
      public interface Interface1
          extends Iterable<CharSequence>{ // this part fails

          public java.util.List<? extends CharSequence> getList(); // this part works.
      }

      public interface Interface2
          extends Iterable<Appendable>{ // this part fails

          public java.util.List<? extends Appendable> getList(); // this part works.
      }


      class NewClass implements Interface1, Interface2{
          private final List<StringBuffer> inner = new ArrayList<StringBuffer>();

          public List<StringBuffer> getList() { // compiles
              return inner;
          }
          
          public Iterator<StringBuffer> iterator() { // fails compile
              return inner.iterator();
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      A new interface must be written which inherits all top level interface, and passed as a parameterized argument to the sub-interfaces. The example above might look like this :

      public interface Combined extends CharSequence, Appendable {}

      public interface Interface1<T extends CharSequence extends Iterable<T>{
          public java.util.List<? extends CharSequence> getList(); // this part works.
      }

      public interface Interface2<T extends Appendable extends Iterable<T>{
          public java.util.List<? extends Appendable> getList(); // this part works.
      }

            ahe Peter Ahe
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: