• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting rid of generics warnings

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read this and I think that it's absolutely great. Now I'm trying to implement my own version which works fine, but I can't seem to get rid of some generics warnings.

My class looks like (simplified, evil stands for ev al which the forum doesn't allow ):


This gives me the following warning:
Type safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable<T> should be parameterized

Parameterizing to the following:

Gives me the warning:
Type safety: The cast from Object to T is actually checking against the erased type Object

How can I get rid of these warnings without using @SuppressWarnings?

Thanks in advance.
 
Michael Herrmann
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Noone has got an idea?
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compile with the -nowarn flag.

Example:
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you've done here is found one of the pitfalls of the now broken Comparable interface. This interface is broken because of 'generics by type erasure', which is the underlying flaw.

How you might ask? interface Comparable should have been interface Comparable<T>{T compareTo(T t);} (actually, the method *should* declare to throw NullPointerException, which is the optimal (but not ideal) workaround to the existance of 'null', which is a language design flaw). So why wasn't it done like that? Because it would break existing clients, so instead, we have a very broken interface, which is intrinsic to a very broken collections API (in many other ways).

What can you do about it? Rewrite collections (as I have done partially), or not use Java - it's that simple (I say this because perservering with the compile-time warning fits the category of "not use Java" afaic). At some point, you have to concede to the brokenness; acknowledging it is important in making informed decisions. Acknowledging it is often misconceived as cynicism or what have you, but that's just the internet

Here's something I wrote that may be of interest: http://qa.jtiger.org/GetQAndA.action?qids=70&showAnswers=true


Edit:
Gah! I just reread my post.
"should have been interface Comparable<T extends Comparable>{int compareTo(T t);}" is what I meant - sorry for any confusion.
[ July 08, 2005: Message edited by: Tony Morris ]
 
Michael Herrmann
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so there's no choice but to suppress the warnings. I'll do that, thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic