> > It appears that the problem with my version is that although we have
> > stipulated that the type of x extends Comparable<T1A super T1> and the
[quoted text clipped - 8 lines]
> In that case there would be no required relation between "T2"
> and T1 at all.
Sorry, my reference wasn't clear at all. I had originally written
(privately, not in my post) the same class class declaration and
method signature that you had, but in the method body I'd used
public int compareTo(Singleton<? extends Comparable<? super
T1>> s) {
return x.compareTo(s.x);
}
> I don't think more generic bounds syntax would help
> in your case. The bounds I gave above are about as
> complex as the English language description of the
> required type restrictions. Anything more concise than
> that would be obfuscation.
Hmm...let's compare the generics syntax for method signatures and
class declarations. In a method signature, you can declare and place
constraints on type variables that never occur in the method body, and
that never need to be specified by the API client. This makes code
like the following possible:
public static <T extends Comparable<? super T>, T1 extends T, T2
extends T> int compare(T1 x1, T2 x2) {
return x1.compareTo(x2);
}
public static void main(String[] args) {
Date x1 = new Date(1);
Timestamp x2 = new Timestamp(3);
int i = compare(x1,x2);
}
IMHO, the type variable declaration in this static compare method is
extremely clear -- the two argument types must have a common supertype
that is Comparable with itself. Note that the type variable T exists
solely to specify the constraints on the common supertype. It never
appears in the method body, and the API client never needs to worry
about it.
As far as I can tell, there is no way to do something analogous in a
class declaration, because each type variable declared must be
specified by the client upon type declaration. What we'd really want,
as Jesper pointed out, is something that allows the introduction of
new type variables in the class declaration that do not need to be
specified by the API client at type declaration. This could be
something like what I originally wrote:
public class Singleton<T1 extends Comparable<? super T1>> implements
Comparable<Singleton<T2>> {
T1 x;
public int compareTo(Singleton<T2> s) {
return x.compareTo(s.x);
}
}
or something like what Jesper wrote:
public class Singleton<T1 extends Comparable<T2>> implements
Comparable<Singleton<? extends T2>> { ... }
I appreciate all the input on my problem -- I understand the issues
much better now!
Cheers,
Roger
Larry Barowski - 15 Oct 2004 20:59 GMT
> Hmm...let's compare the generics syntax for method signatures and
> class declarations. In a method signature, you can declare and place
[quoted text clipped - 10 lines]
> As far as I can tell, there is no way to do something analogous in a
> class declaration
Ah, I see your point. Something like that would add a lot more power
to the bounds.