Forums Register Login

erasure...Generics

+Pie Number of slices to send: Send


when I complile this file without generics everything is cool...but with Generics it complains about unchecked or unsafe operations..why is it like that
+Pie Number of slices to send: Send
Welcome to the Ranch.

Does it only complain about line 3? Somebody else has the same problem, and hasn't worked it out yet: here. Why do you think you are getting the warning?
+Pie Number of slices to send: Send
This article throws some light on a similar warning.

I quote this from the article:

Why is there a compiler warning? Because in order to pass a and b to the method the varargs implementation needs to create an array - Collection<String>[]. But the generics implementation can't create a generified array, yet thats what the method is demanding. Arrrgh!!

So, instead of being helpful, Java creates an ungenerified Collection[], and then the generics system warns us that it is an unchecked cast to make it a Collection<String>[]. But this conversion is all internal to the compiler! As far as the developer is concerned, there is nothing even vaguely type unsafe about what s/he is doing.

Now this is just headbangingly stupid. There is absolutely no risk of breaking the generics model here. No risk of a ClassCastException or ArrayStoreException. No, this is really just another case of generics being nearly completely broken with regards to arrays. And the varargs group didn't manage to realise/coordinate and get a decent implementation that works.

The problem for me is serious though. I am writing an API (commons-collections) that will be widely used, and will have to retain backwards binary compatibility. If I go with the varargs option (the logically correct choice), then every client of this method will get a useless java warning - not very friendly. The alternative is to go back to the original design and have three methods, an array, one-arg and two arg. And really thats just crap.



So, it seems it is basically because of the var-args method asList() trying to create a generics array in order to call the var-args method.
Since, the compiler generates this code, on compilation, we do not even get the line number for the warnings. Now that is something really ridiculous!!!
However, if you are really bothered about it, put this annotation at the top of your method:

+Pie Number of slices to send: Send
Thank you very much, Nitesh Kant. I think it has something to do with Java generics using type erasure rather than reification, but other people will know a lot better than me.
+Pie Number of slices to send: Send
 

Campbell: I think it has something to do with Java generics using type erasure rather than reification, but other people will know a lot better than me.



can you explain a little as to what you perceive the problem is.
+Pie Number of slices to send: Send
No, I am afraid I can't explain. I know that Java generics is done by type erasure (look in the Java Tutorials for "generics") which means that after the compiler has checked all the types, the types are deleted and the Lists etc are non-generic. Somehow that means it is impossible to create an array of generic objects; It has come up on these boards before, so doing a search for "type erasure generic array" might help. This thread which might be helpful, came up when I did exactly that search. See whether that helps. I am afraid that is as far as I can help.

[edit]Corrected misspelt hyperlink[/edit]
[ January 28, 2008: Message edited by: Campbell Ritchie ]
He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 864 times.
Similar Threads
Generics..
explain the unchecked warning that is given in line 3 and why it occurs:
generics
Generics
generics
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 07:59:52.