Improved for each loop

M

markspace

Tom said:
The only problem is that use of it involves a box-unbox on every iteration.

The new syntax would be pure syntactic sugar - it wouldn't require any
new classes, or bytecodes, or class format changes, or changes to any
other part of the language.


That was at least part of the idea: no new byte codes or classes, and
the efficient integer primitive is used.
 
L

Lew

Tom said:
Not being able to for-loop over an Iterator, as opposed to an Iterable,
is also incredibly frustrating. I start with something like this:

for (String s: someCollectionOfStrings) {
fooBarDoStuff();
doSomethingOnlyForTheLastElement(); // needs a guard
}

And then i [sic] realsie that i [sic] can't do that - i [sic] have to rewrite the loop as
a while loop. Like:

Iterator<String> it = someCollectionOfStrings.iterator();
while (it.hasNext()) {
s = it.next()) {
fooBarDoStuff();
if (it.hasNext()) doSomethingOnlyForTheLastElement();
}

Which feels much less cohesive and more clunky to me.

If you want to use a traditional three-part for loop, you have to do
something bonkers like:

String s;
for (Iterator<String> it = someCollectionOfStrings.iterator();
it.hasNext() && ((s = it.next()) != null);) {
fooBarDoStuff();
if (it.hasNext()) doSomethingOnlyForTheLastElement();
}

First of all, what you did isn't all that "bonkers"; you've just been spoiled
by the convenience of for-each over Iterables.

Second, under most circumstances you'd declare the String inside the loop, not
outside. That's what would match a hypothetical for-each over Iterators anyway:

for ( Iterator <String> it = someCollectionOfStrings.iterator();
it.hasNext(); // nullity should have been prevented on insert
)
{
String s = it.next();
doStuff( s );
}

That can be compressed if it's that simple:

for ( Iterator <String> it = someCollectionOfStrings.iterator();
it.hasNext(); // nullity should have been prevented on insert
doStuff( it.next() )
)
{
}

Finally, if you start with 'someCollectionOfStrings' in the first place, why
do you need an explicit Iterator at all?

for( String s : someCollectionOfStrings )
{
doStuff( s );
}

The whole point of the for-each syntax is to spare you from retrieving the
Iterator. If you're retrieving the Iterator, then you don't need for-each
anyway. Just use one of the other two for-loop constructs I just illustrated.
 
Joined
Mar 23, 2009
Messages
8
Reaction score
0
In reply to the primary question, while the for each that iterates an integer range is a nifty idea, is it really that necessary? A while(i != 42) i++; would work just as simply, I think the for each should be saved for iteratable objects.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,076
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top