But as I said, that example is bollocks code.
If I saw that in some real code I would take it out back and have it shot.
How should the compiler know that the above is not a valid cast?
And what...
Type: Posts; User: fortioni
But as I said, that example is bollocks code.
If I saw that in some real code I would take it out back and have it shot.
How should the compiler know that the above is not a valid cast?
And what...
Show us an example then...as code.
I presume you have encountered something that does cause a problem in real life, and not merely as a mental exercise, that you could use here to illustrate your...
Why should the cast to Object[] be flagged though?
Will you flag casts to List<SomeClass>[] as well?
How about any cast?
Because that is essentially what you are suggesting...casting is always...
We're obviously talking at cross-purposes.
The array assignment marked as Valid in the code snippet from the docs has always been valid, and is valid for things that do not involve generics....
Which has nothing to do with type erasure, since that is something specific to generics, and:
Java Code:
Object[] objects = new SomeClass[]();
predates them by several years.
You are...
But that would not fix the issue of casting SomeClass[] to an Object[], which has nothing at all to do with type erasure.
Which is the point I was replying to.
The above that you complained about...
No it shouldn't.
Casting an array of something to an Object[] is not something that needs checking since everything is rooted in the Object class.
It's possibly a side effect of the nature of...
The line labelled valid will never give an unchecked warning, even if the parameter were a List<String>[].
Java Code:
private static void test(List<String>[] l) {
Object[] o = l; // This...
I don't know if it's the only solution, but it is the reasoning that the documentations give:
"Type erasure enables Java applications that use generics to maintain binary compatibility with Java...
I gave it some more thoughts, but, suppose the requirements were:
a) old code should keep on compiling and
b) old code should keep on running and
c) generics are supposed to be a compile time...
Ok, I reword that phrase: generics are a compile time issue in Java and hence its weakness. OTOH, when you want to go all the way, you end up with possible 'code bloat' as in C++'s templates; I guess...
I'm not sure what your issue is. It doesn't surprise me that Java doesn't complain about the add since the List variable, l, is not generic (and the compiler even warns you of this problem). If you...
Yep, the generated code is the same as before generics saw the light. Those casts are still needed during runtime because of the 'backward compatibility'. I don't understand your remark on 'heap...
No we can't because the compiler doesn't generate any new code at all; Generics is a compile time thingy; even the runtime casts are still present, even if the compiler 'knows' that a generic type is...