Re: No coercion in for-each loop?



In article <1193566420.335822.13500@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
crazzybugger <ajaykumarns@xxxxxxxxx> wrote:
On Oct 28, 5:31 am, Dave Stallard <stall...@xxxxxxxxxx> wrote:
List ar = new ArrayList();
ar.add(1);
List<String> list = ar;
for (String s : list)
Utils.log(s);

It is true that the compiler does not mind when you code
List<String> list=ar even though ar may contain non String objects. It
does so because, you are knowingly doing it and telling the compiler,
"See compiler, I am sure this list contains only String items and if
any error occurs I will manage it".

I don't see that you are saying this in the above code. What you are
saying is "As far as I recall, 'ar' is actually a List<String> so this
should work a treat" - much as if you had written
Object o = ...;
String text = o;

The compiler doesn't accept the latter, why should it accept the
former? Shouldn't it /require/ you to write
List<String> list = (List<String>) ar;
in the same way that it requires
String text = (String) o;
?

(Not that the cast would do you any good though, since
List ar = new ArrayList<Integer>();
ar.add(1);
List<String> list = (List<String>) ar;
for (String s : list)
System.out.println(s);

doesn't actually barf on the cast, but on the for statement. This
might be the reason I suppose - Java doesn't require the explicit cast
because it's unable to verify it anyway. This doesn't really improve
the situation in my eyes. But we may just be rehashing old complaints
about the generics system here.)

Cheers,
Bent D
--
Bent Dalager - bcd@xxxxxxx - http://www.pvv.org/~bcd
powered by emacs
.



Relevant Pages

  • Re: C# Nullable types
    ... string x = null; ... compiler that by using the language specific word called "null". ... so we are given a keyword to state our intentions as the ... I am saying that I don't want x to have any value and I am "ok" with that. ...
    (microsoft.public.dotnet.framework)
  • Re: Multiple Inheritance with Interfaces
    ... I cast one to the Interface it compiled, ... it compiles ok only because the java compiler does not ... It is very hard for a compiler to determine what to do in this case, because a carefully timed thread could plant a String into mumble between line 1 and line 2, allowing line 2 to execute without error. ... but with reference expression treated as being capable of referencing ...
    (comp.lang.java.programmer)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... Big-O notation isn't mathematics per se, it's computer science notation ... "length of the string". ... outrun something compiled and optimized by a good C compiler. ... > either general computing culture or culture outside computing. ...
    (comp.programming)
  • Re: [EGN] Hoisting Loop Invariants (Was: Re: [EGN] Numerical Accuracy)
    ... compiler out there somewhere that did as you claim. ... > the programmer has this knowledge, then the programmer should not use ... >> string in a loop, regardless of the blatant inefficiency of doing so. ...
    (comp.programming)
  • Re: PL/I string representations
    ... >> of the language, so it was interesting to me, hopefully it will be to ... I found a workable compiler for Fortran in 1971 (with a bug ... >> The specified length is the minimum, and each time a character ... >> string is assigned to E, the length is stored with it. ...
    (comp.programming)