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

Remi Forax forax at
Wed Mar 6 12:37:37 UTC 2019

Hi Tagir,
try to do it now and you will see that you can't, because you can not write Iterable<int> yet.
Once we will get generics over value types, it will be a no-brainer.


----- Mail original -----
> De: "Tagir Valeev" <amaembo at>
> À: "Stuart Marks" <stuart.marks at>
> Cc: "core-libs-dev" <core-libs-dev at>
> Envoyé: Mercredi 6 Mars 2019 11:10:41
> Objet: Re: Proposal: JDK-8148917 Enhanced-For Statement Should Allow Streams

> Hello!
> By the way one of the painful code patterns in modern Java is `for(int
> i = 0; i<bound; i++)` which is very verbose, hackish, confusing for
> newbies and prone to errors as the variable need to be repeated three
> times. Also the variable is not effectively final, despite it never
> changes within the loop body, so could have been considered as
> redeclared on every loop iteration (like in for-each). With the new
> proposal it's possible to write `for(int i : range(0, bound).boxed())`
> (assuming import static j.u.s.IntStream.range), which looks much
> better, though it has obvious performance drawback. Moving
> IterableOnce to BaseStream would enable to use `for(int i : range(0,
> bound))` which looks even better, though again we have plenty of
> garbage (but considerably less than in previous case!). I wonder
> whether Java could evolve to the point where such kind of code would
> be a recommended way to iterate over subsequent integer values without
> any performance handicap.
> With best regards,
> Tagir Valeev.
> On Fri, Mar 1, 2019 at 9:47 AM Stuart Marks <stuart.marks at> 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:
>> Bug report:
>> Webrev:
>> 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