Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34763
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: mollifying generics |
| Date | 2016-07-05 22:28 -0400 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <nlhqck$cg0$1@gioia.aioe.org> (permalink) |
| References | <o907nblakke2c75jrjdbav4pauap3jihg9@4ax.com> |
On 6/29/2016 4:11 AM, Roedy Green wrote:
> Prior to generics, I could say something like this:
>
> Comparable a = "abc";
> Comparable b = "def";
>
> or Comparable a = new Long( 2 );
> Comparable b = new Long ( 4 );
>
>
> int diff = a.compareTo(b);
>
> The run time would use the corresponding compareTo method
>
> Now generics complains about an unchecked operation.
Yes.
And that is good.
The old code is not type safe.
public class ToComOrNotToComp {
public static void main(String[] args) {
Comparable a = "abc";
Comparable b = "def";
System.out.println(a.compareTo(b));
Comparable c = new Long(2);
Comparable d = new Long(4);
System.out.println(c.compareTo(d));
System.out.println(a.compareTo(d));
}
}
gives 7 warning, but also an exception at runtime.
> Is there a way to mollify it? I have tried what I thought were the
> obvious means.
I don't think there are any good solution, because I don't think
there should be a good solution.
A Comparable<X> can be compared to a X.
But I don't think one can assume that a Comparable<X> can be
compared to a Comparable<X>.
Or to rephrase it: your assignments are losing too much
information to make comparison feasible.
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
mollifying generics Roedy Green <inquiry@mindprod.com> - 2016-06-29 01:11 -0700
Re: mollifying generics Andreas Leitgeb <avl@logic.at> - 2016-06-29 08:33 +0000
Re: mollifying generics Daniel Pitts <usenet@coloraura.com> - 2016-06-29 13:43 -0700
Re: mollifying generics Daniel Pitts <usenet@coloraura.com> - 2016-06-29 13:47 -0700
Re: mollifying generics Arne Vajhøj <arne@vajhoej.dk> - 2016-07-05 22:28 -0400
csiph-web