My thoughts as an enterprise Java developer.

Friday, March 30, 2007

Automatic casts

Why can't the compiler automatically insert casts?
List list = new ArrayList();
list.add("one");
list.add("two");
for(Iterator i = list.iterator(); i.hasNext(); ) {
String item = i.next();//Automatic casts
System.out.println(item);
}
//Or using the new for construct:
for(String item : list) {
System.out.println(item);
}

http://weblogs.java.net/blog/emcmanus/archive/2007/03/getting_rid_of.html shows that the compiler can get really close. Why can't it go the rest of the way?

No comments: