Proposal: JDK-8148917 Enhanced-For Statement Should Allow Streams

Stuart Marks stuart.marks at
Tue Mar 12 21:59:03 UTC 2019

Hi Stephen,

> My slight concern is that the terminal operation is hidden and not
> immediately visible, which could be surprising. I do note that streams
> throw a clear exception if a terminal operation is called a second
> time which mitigates this to some degree.

Yes, this is certainly a possibility.

I'll note that even though my example does it, I think that storing a stream in 
a local variable is a bit of a code smell. It has to be done for 
try-with-resources, but storing the head of a pipeline in a local variable so 
that it can be iterated over does introduce the possibility of accidental reuse.

I suspect that most cases (aside from try-with-resources) will call a method 
returning a fresh Stream that's then iterated over. For example,

     for (var x : getSomeStream()) {
        // ...

In this case there's no possibility of accidental reuse.

> The IterableOnce class-level Javadoc contradicts the method-level
> Javadoc. The class-level say "It is recommended that" whereas the
> method-level Javadoc mandates.

Oh yes, thanks. I'll fix this to make the behavior mandatory.


More information about the core-libs-dev mailing list