18.2.2 Type Compatibility Constraints 0.7.0 changes
Dan Smith
daniel.smith at oracle.com
Thu Nov 21 10:25:31 PST 2013
On Nov 19, 2013, at 3:20 AM, Anna Kozlova <Anna.Kozlova at jetbrains.com> wrote:
> In 0.7.0 spec "if T is a parameterized type of the form G<T1, ..., Tn>, and
> there exists no type of the form G<S1, ..., Sn> that is a supertype of S,
> but the raw type G is a supertype of S, then the constraint reduces to true"
> was added. How does javac then infer raw substitution here?
>
>
>
> class Test {
>
> interface Condition<K> {}
>
> class SpecialCondition<M> implements Condition {}
>
> static <T> List<T> filter(T[] c, Condition<? super T> con) {
> return null;
> }
>
> void foo(CharSequence[] es, SpecialCondition<CharSequence> con) {
> List<String> l = filter(es, con);
> }
>
> }
So we have
SpecialCondition<CharSequence> -> Condition<? super t>
And, per 18.2.2 (as quoted above), this reduces to true.
18.5.1 concludes that the method is applicable, with bound set { t <: Object, CharSequence <: t }.
For invocation typing, we have this clause in 15.12.2.6:
"Otherwise, if unchecked conversion was necessary for the method to be applicable, then the invocation type is the erasure (4.6) of the method's type."
Was unchecked conversion necessary to demonstrate the method's applicability? Yes. (This could be more clear; hence the "to do" item in 18.5.2.)
So, per 15.12.2.6, the method returns List, and the assignment allows an unchecked conversion to List<String>.
—Dan
More information about the lambda-dev
mailing list