• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting rid of generics warnings

 
Michael Herrmann
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Noone has got an idea?
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compile with the -nowarn flag.

Example:
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Ok, so there's no choice but to suppress the warnings. I'll do that, thanks.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic