Problem of defender methods with generic methods
Steven Simpson
ss at comp.lancs.ac.uk
Mon Nov 15 00:55:07 PST 2010
Hi,
On 06/11/10 04:55, Ali Ebrahimi wrote:
> public interface CustomizedList<T> extends List<T> {
> <A extends Comparable<? super A>> extension A max() default
> Trait.<A>max; <============= cannot find symbol A
What is this supposed to mean, especially to a caller? AIUI,
CustomizedList.max will appear to the caller as a regular virtual
method, and the caller has no knowlegde that it is somehow bound to
Trait.max.
> public static class Trait {
> public static <R extends Comparable<? super R>> R max(
> final CustomizedList<? extends R> self) {
Okay, Trait.max is defined so that it is only valid on CustomizedLists
of Comparables, though this isn't directly visible to the caller of
CustomizedList.max. I suppose that the declaration of
CustomizedList.max aims to indicate that it cannot be called if the
element type is not Comparable, to ensure that the constraints on
Trait.max will be met.
So what's the full signature of CustomizedList.max? Simply stripping
out the extension syntax leaves this:
<A extends Comparable<? super A>> A max();
But that doesn't place any extra restriction on <T>, and would allow the
caller to write:
CustomizedList<Runnable> list = ...;
String result = list.max(); // compiles
However, when compiling CustomizedList, it is seen (approximately) that
<A> must correspond to <R>, and that <R> must correspond to <T>, so <A>
must correspond to <T>, so there's enough information in the declaration
of CustomizedList.max to show that there are further constraints on <T>
- but this information is not directly available to the caller unless
the compiler makes it available through the method's signature while it
has the opportunity. But what can this new signature for
CustomizedList.max be? AFAIK, there's no syntax that expresses
additional constraints on the class's type parameters.
I believe that redeclaring <T> actually introduces a new <T> which hides
the class's <T>, so it's no better than <A>:
<T extends Comparable<? super T>> T max();
CustomizedList<Runnable> list = ...;
String result = list.max(); // still compiles
Has a 'reconstraining' syntax been introduced somewhere? If not, I
think one is necessary to deal with these cases, if such cases are to be
permitted.
Cheers,
Steven
More information about the lambda-dev
mailing list