C
Carl Howells
It seems that the extended for syntax added in Jdk 1.5 has one big issue
making it more difficult to use than it should be.
Consider the very useful new input class, java.util.Scanner. Scanner
implements the Iterator<String> interface. That means that if I just
want to tokenize a stream based on a fixed delimiter pattern, treating
all the tokens as String objects, I can just treat the Scanner as an
Iterater<String>, and be perfectly happy with the results.
But I can't do that with the new extended for loop syntax. Here's an
example of what I'd like to do, but currently can't:
import java.util.*;
public class Test
{
public static void main(String [] args) throws Exception
{
Scanner sc = Scanner.create(System.in);
for (String s : sc)
{
System.out.printf("'%s'\n", s);
}
}
}
The problem is that the extended for syntax only allows instances of
Iterable (And arrays... Have those been changed to implement Iterable,
or does the syntax allow arrays as well as Iterable objects?) as the
object to iterate over. This seems unnecessarily restrictive. This led
to me writing some exceptionally stupid code just to be able to use the
much cleaner for syntax:
import java.util.*;
public class Test
{
public static void main(String [] args) throws Exception
{
Scanner sc = Scanner.create(System.in);
for (String s : new IteratorReturner<String>(sc))
{
System.out.printf("'%s'\n", s);
}
}
static class IteratorReturner<E> implements Iterable<E>
{
private Iterator<E> it;
IteratorReturner(Iterator<E> i)
{
it = i;
}
public Iterator<E> iterator()
{
return it;
}
}
}
I mean, that's just stupid. The extra class adds no semantic value. It
doesn't make things clearer. It's just a stupid hack to make the types
what the compiler wants.
Consider how many API's return instances of Iterator or Enumeration,
rather than a collection that implements Iterable. Why is having an
adaptor class like the one needed above a cleaner solution than just
allowing the extended for syntax to iterator over instances of Iterator
and Enumeration?
Does anyone else think that this is a big enough issue that it should be
fixed? Is there anything about this in Sun's bug or RFE databases? I
tried searching, but the search engine for those databases is severely
limited.
making it more difficult to use than it should be.
Consider the very useful new input class, java.util.Scanner. Scanner
implements the Iterator<String> interface. That means that if I just
want to tokenize a stream based on a fixed delimiter pattern, treating
all the tokens as String objects, I can just treat the Scanner as an
Iterater<String>, and be perfectly happy with the results.
But I can't do that with the new extended for loop syntax. Here's an
example of what I'd like to do, but currently can't:
import java.util.*;
public class Test
{
public static void main(String [] args) throws Exception
{
Scanner sc = Scanner.create(System.in);
for (String s : sc)
{
System.out.printf("'%s'\n", s);
}
}
}
The problem is that the extended for syntax only allows instances of
Iterable (And arrays... Have those been changed to implement Iterable,
or does the syntax allow arrays as well as Iterable objects?) as the
object to iterate over. This seems unnecessarily restrictive. This led
to me writing some exceptionally stupid code just to be able to use the
much cleaner for syntax:
import java.util.*;
public class Test
{
public static void main(String [] args) throws Exception
{
Scanner sc = Scanner.create(System.in);
for (String s : new IteratorReturner<String>(sc))
{
System.out.printf("'%s'\n", s);
}
}
static class IteratorReturner<E> implements Iterable<E>
{
private Iterator<E> it;
IteratorReturner(Iterator<E> i)
{
it = i;
}
public Iterator<E> iterator()
{
return it;
}
}
}
I mean, that's just stupid. The extra class adds no semantic value. It
doesn't make things clearer. It's just a stupid hack to make the types
what the compiler wants.
Consider how many API's return instances of Iterator or Enumeration,
rather than a collection that implements Iterable. Why is having an
adaptor class like the one needed above a cleaner solution than just
allowing the extended for syntax to iterator over instances of Iterator
and Enumeration?
Does anyone else think that this is a big enough issue that it should be
fixed? Is there anything about this in Sun's bug or RFE databases? I
tried searching, but the search engine for those databases is severely
limited.