Problem of defender methods with generic methods
Ali Ebrahimi
ali.ebrahimi1781 at gmail.com
Mon Nov 15 20:58:29 PST 2010
Hi Alex,
Comments inlined.
On Tue, Nov 16, 2010 at 2:20 AM, Alex Buckley <alex.buckley at oracle.com>wrote:
> Hi Ali,
>
>
> On 11/15/2010 2:51 AM, Ali Ebrahimi wrote:
>
>> if compiler can accept the following syntax:
>>
>> <A extends T & Comparable<? super A>> extension A max() default
>> Trait.<A>max;
>>
>> The aim is here to restrict calling of max only on CustomizedLists of
>> Comparables, otherwise compiler raise compile time error.
>>
>> I know this is a new concept- a kind of conditional inheritance. but
>> currently this can not be implemented in java, unless by introducing a new
>> subinterface. then we have two interfaces.
>>
>
> You and Steven have identified a valuable idiom that avoids new subtypes -
> we can call it "conditional inheritance" if you like - but unfortunately the
> generics mechanism cannot express it. Namely, the bound of a type variable
> can be _either_ another type variable _or_ a combination of classes and
> interfaces. (See JLS3 4.4.)
>
> The trouble is erasure: if a type variable's bound could be 'T &
> Comparable<...>', then it would not be clear whether to erase max's return
> type to Object (the erasure of T) or Comparable (the erasure of
> Comparable<...>). I guess we could pick the former, but the
I prefer later, since Comparable implicitly is subtype of Object.
> Signature attribute would want to record both T and Comparable<..> for
> later typechecking of A's witness from the call site. (Does it subtype
> List's type argument _and_ Comparable<..> ?)
>
> JLS3 4.4 arranged things to ensure a unique erasure, so it's not just a
> matter of relaxing syntax to allow your desired type variable for max.
>
> I personally believe we will want to add extension methods that further
> restrict the enclosing generic type's bounds, and that a new subinterface
> (ComparableList<T extends...>) will often be undesirable. But more
> experience is needed.
>
> Alex
>
Best Regards,
Ali Ebrahimi
More information about the lambda-dev
mailing list