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

Peter Levart peter.levart at gmail.com
Wed Mar 6 15:50:06 UTC 2019


Hi Stuart,

According to Liskov substitution principle:

     Subtype Requirement: Let ϕ ( x ) be a property provable about 
objects x of type T. Then ϕ ( y ) should be true for objects y of type S 
where S is a subtype of T.


Let ϕ ( x ) for objects x of type Iterable be: "x.iterator() may be 
invoked multiple times, each time starting new iteration".

This clearly holds.

Does ϕ ( y ) hold for objects y of type IterableOnce? Clearly not.

In this respect Iterable should be a subtype of IterableOnce and foreach 
loop should be retrofitted to work with IterableOnce.

What do you think?

Regards, Peter

On 3/1/19 3:43 AM, Stuart Marks wrote:
> Hi all,
>
> Please review and comment on this proposal to allow Stream instances 
> to be used in enhanced-for ("for-each") loops.
>
> Abstract
>
> Occasionally it's useful to iterate a Stream using a conventional 
> loop. However, the Stream interface doesn't implement Iterable, and 
> therefore streams cannot be used with the enhanced-for statement. This 
> is a proposal to remedy that situation by introducing a new interface 
> IterableOnce that is a subtype of Iterable, and then retrofitting the 
> Stream interface to implement it. Other JDK classes will also be 
> retrofitted to implement IterableOnce.
>
> Full Proposal:
>
> http://cr.openjdk.java.net/~smarks/reviews/8148917/IterableOnce0.html
>
> Bug report:
>
>     https://bugs.openjdk.java.net/browse/JDK-8148917
>
> Webrev:
>
>     http://cr.openjdk.java.net/~smarks/reviews/8148917/webrev.0/
>
> Note, this changeset isn't ready to push yet. In particular, it has no 
> tests yet. However, the implementation is so simple that I figured I 
> should include it. Comments on the specification wording are also 
> welcome.
>
> Thanks,
>
> s'marks



More information about the core-libs-dev mailing list