JDK-6176774 : arrays should implement Iterable
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0,6
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-10-11
  • Updated: 2011-10-31
  • Resolved: 2011-10-31
Related Reports
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
The new "foreach" loop works with arrays and Iterable, but these two are treated differently and it is not possible to write a function that accepts both an array and an iterable (as does the "foreach" loop).

JUSTIFICATION :
It is cleaner and more flexible to handle iteration over arrays in exactly the same way as iteration over collections.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I would expect that this function accepts Iterable<Byte>, Byte[] and, with the help of autoboxing, also byte[]:

void dumpBytes(Iterable<Byte> bytes) {
  for( Byte b : bytes) {
  }
}

If arrays would implement Iterable this could work.

CUSTOMER SUBMITTED WORKAROUND :
Currently you have to write 3 functions with different signature but identical implementation:

void dumpBytes(Iterable<Byte> bytes) {
  for( Byte b : bytes) {
  }
}

void dumpBytes(Byte[] bytes) {
  for( Byte b : bytes) {
  }
}

void dumpBytes(byte[] bytes) {
  for( Byte b : bytes) {
  }
}
###@###.### 10/11/04 05:00 GMT

Comments
EVALUATION Proposals for new features in the Java programming language are no longer being accepted or maintained in the bug database at http://bugs.sun.com/. This also applies to proposals for new features in the Java Virtual Machine (that is, in the abstract JVM, as opposed to a JVM implementation like HotSpot). All proposals for new features should be made through the "JDK Enhancement - Proposal & Roadmap Process" described at http://openjdk.java.net/jeps/1. This specific request to change the Java programming language is related to the work of OpenJDK Project Lambda. It will be considered further by that project, not here. The bug database continues to accept and maintain submissions about technical errors in the design and specification of the Java programming language and the Java Virtual Machine.
31-10-2011

EVALUATION Auto-converting Byte[] to Iterable<Byte> is quite something. It's like boxing conversion on steroids. Auto-converting Byte[] to List<Byte> would be even more interesting. It would allow a modern generified library to be called by old clients (that pass arrays). It would be a companion to the ability to use array access on collections (see 4877954).
17-11-2006

EVALUATION I think this is a specification issue. Please submit a compiler bug if this is accepted. ###@###.### 10/18/04 17:31 GMT
18-10-2004