PROPOSAL: Enhanced for each loop iteration control
Schulz, Stefan
schulz at e-Spirit.de
Mon Mar 30 23:35:25 PDT 2009
Stephen Colebourne wrote:
> Stefan Schulz wrote:
> > Frankly, I cannot see a great advantage of:
> > for (Foo foo : fooList : it) {
> > ...
> > }
> > saving two lines of code over:
> > Iterator<Foo> it = fooList.iterator();
> > while (it.hasNext()) {
> > Foo foo = it.next();
> > ...
> > }
>
> The former captures the *intent* of the loop. The latter is all about
> plumbing.
I know that, Stephen. I don't mind adding some convenience syntax at all. My comment included the overhead being produced.
> > by adding two wrappers and stuff to the code in the background.
>
> The proposal discusses possible optimisations, however I suspect that
> hohtspot can probably cope with two additional objects being created.
>
> Remember, the extra overhead only happens if you add the optional
> iterator reference.
Well, if you give developers a tool, they use it.
I am thinking of nested loops using this feature and the implications on instance creations the developer does not recognize when applying iteration control. One could as well assign the constructed iterator directly to "it", only the compiler loses some control on the iteration. And that's not worth the overhead, IMHO.
Uniforming array access on the iteration control seems nice, though.
Stefan
More information about the coin-dev
mailing list