Proposal: JDK-8148917 Enhanced-For Statement Should Allow Streams
swpalmer at gmail.com
Wed Mar 6 14:48:47 UTC 2019
I don’t mean any offence, but I have to say, I strongly disagree with nearly everything you’ve written below. To me, the idea of making a stream of integers for a simple loop counter is hackish, confusing, verbose, and basically abusing the stream concept. The only part I agree with is that it is an obvious performance drawback as well. A counter and a stream of integers are completely different concepts and should not be confused in this manner.
> On Mar 6, 2019, at 5:10 AM, Tagir Valeev <amaembo at gmail.com> wrote:
> 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 oracle.com> wrote:
>> Hi all,
>> Please review and comment on this proposal to allow Stream instances to be used
>> in enhanced-for ("for-each") loops.
>> 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:
>> 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.
More information about the core-libs-dev