The prevailing assumption about the Iterable interface is that such instances be independent of each other, that they not interfere with each other or with the underlying state of the implementing object, and that it be possible to create an arbitrary number of Iterators using successive calls to the iterator() method.
However, the specification of Iterable is silent about all of these issues. While the semantics cannot be altered compatibly, a recommendation with the above could be added as an API note.
The java.nio.file.DirectoryStream interface is a notable outlier here, as it implements Iterable but only one Iterator can be obtained from it. Since it's the outlier, it has a large warning to this effect.
However, the specification of Iterable is silent about all of these issues. While the semantics cannot be altered compatibly, a recommendation with the above could be added as an API note.
The java.nio.file.DirectoryStream interface is a notable outlier here, as it implements Iterable but only one Iterator can be obtained from it. Since it's the outlier, it has a large warning to this effect.
- relates to
-
JDK-8148917 enhanced-for statement should allow streams
-
- Open
-