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

Brian Goetz brian.goetz at
Tue Mar 12 16:04:16 UTC 2019

No.  You have the LSP backwards (though this is easy to do.)

IterableOnce means "*must throw* on subsequent use"; under this spec, an 
arbitrary Iterable is most certainly *not* an IterableOnce, and 
therefore an LSP violation.

It sounds like you are suggesting that we instead spec 
IterableAtLeastOnce, of which Iterable *would* be a credible subtype -- 
but due to how Iterable is specified now, the problem is that Iterable 
*already is* really "iterable at least once."  So there would be no 
point in introducing this type; we already have it.

If we were doing this from scratch, we might choose a different path, 
but that option is not open to us.  As several have already noted, this 
proposal is not ideal, but due to the corner we're painted into by 
Iterable's current spec, there is not going to be an ideal outcome -- 
and the current strategy (do nothing) is also not ideal and makes many 
people unhappy.  So the game here is finding the least-bad solution that 
is compatible with the constraints we have, which I think Stuart has 
done exactly. (Also, he did a very nice job of writing up all the 
alternatives, to avoid (or at least reduce) the torrent of "have you 
thought about <thing that has been thought about extensively>".)

On 3/6/2019 10:50 AM, Peter Levart wrote:
> In this respect Iterable should be a subtype of IterableOnce and 
> foreach loop should be retrofitted to work with IterableOnce.

More information about the core-libs-dev mailing list