JDK-6557835 : Iterable should return Iterator rather than Iterator
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 6
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-05-15
  • Updated: 2010-04-04
  • Resolved: 2007-05-15
Related Reports
Duplicate :  
Description
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.
}

Comments
EVALUATION Good idea, unfortunately too late.
15-05-2007