PROPOSAL: Enhanced for each loop iteration control
Stefan Schulz
schulz at e-spirit.de
Mon Mar 30 15:03:30 PDT 2009
I don't particularly like the enriched for each iteration control. While
the J5-for-each loop only adds as syntactic sugar for convenient
looping, iteration control adds overhead to the loop by creating two
instances per for-each, counting the index, etc. Hence, as a developer
in most cases one would be better off with doing the iterator stuff
oneself.
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();
...
}
by adding two wrappers and stuff to the code in the background.
The idea to employ a label to access the iterator I find even worse. A
label does not relate to anything at runtime but a location in code.
Stefan
Marek Kozieł schrieb:
> As you can see i was thinking about it for while:
> http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/001105.html
>
> And i found few problems.
> New iterator Interface is wrong path, because people will be not able
> to use it with their iterators.
>
>
> So from your proposal I would remove Interface-s and link iterator
> through label,
> label type is return type of .iterator() method.
>
>
> like:
>
> ArrayList<String> some ....;
> ...
> i: for (Strign s:some){
> if ( (s==null) || (s.equals("bad")) )) i.remove();
> ...
> i.next(); // compile time error ?
> i.hasNext(); // OK
> Iterator<String> s = i; // error i is not variable (it just allow
> to access it;)
> }
>
> String[] array ...;
> i: for (String s:array){
> i.getIndex;//?
> }
>
>
>
> --
> Pozdrowionka. / Regards.
> Lasu aka Marek Kozieł
>
> http://lasu2string.blogspot.com/
>
More information about the coin-dev
mailing list