Chris Uppal coughed up:
But after erasure (which is not an implementation detail but will
apparently form part of the Java5 language spec), the Comparable<X>
family of interfaces reduce to:
public interface java.lang.Comparable
{
int compareTo(java.lang.Object o);
}
which is why String /must/ have that exact method (not being an
abstract class).
I disagree with that. That's not what erasure means. Erasure *does* in
fact (as you suggest) wipe clean the type information (after the syntax
checking is performed), but it does not /require/ that the stripped down
type (to Object) is honored in itself as an interface contract.
In other words, the results of what happens after the erasure may in fact
look like compareTo(Object) but you could never call it that way because the
compiler only regards that as something it is doing behind your back and not
something you are meant to access as such. If there is no compareTo(Object)
specified by you, there is no compareTo(Object) callable by you.
Erasure will take the <T> parameters out, and insert the down-casts
necessary that may otherwise [probably] be missing. But that's only to get
it to compile. It is merely a mechanism of the compiler to allow backward
compatibility with generics.
In 1.5.0, String.java does /not/ have to have
int compareTo(Object o)
because the generics made it effectively an
int compareTo(String o)
As evidenced by the fact that you cannot call it with an Object object. The
following compiles just fine, with only a compareTo(String) implemented, and
cannot be called with an Object:
<QUOTE from 1.5.0-beta2>
$ cat Gen1.java
package experiments.simple;
public class Gen1
{
public static void main(String[] args)
{
Object str = "hello";
(new Thing()).compareTo(str);
}
}
class Thing implements Comparable<String>
{
public int compareTo(String arg)
{
System.out.println(arg);
return 0;
}
}
$ !com
com Gen1.java
Gen1.java:11: compareTo(java.lang.String) in experiments.simple.Thing cannot
be
applied to (java.lang.Object)
(new Thing()).compareTo(str);
^
1 error
</QUOTE>
....[rip]...
--
Enough is enough. It is /not/ a requirement that someone must google
relentlessly for an answer before posting in usenet. Newsgroups are
for discussions. Discussions do /not/ necessitate prior research. If
you are bothered by someone asking a question without taking time to
look something up, simply do not respond.