RFR: 8177466: Add compiler support for local variable type-inference

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Tue Sep 19 20:21:31 UTC 2017



On 19/09/17 17:58, Sergey Bylokhov wrote:
> On 9/19/17 04:40, Maurizio Cimadamore wrote:
>>
>> So, in neither declarations, both branches of the check are satisfied 
>> - which is why you get an error.
>>>
>>> var s = true ? new String() : new int[1]; // works fine
>>> Is it expected that this code compiles?
>> Now, in this case the conditional is treated as a standalone 
>> conditional - because there's no target type. The type of the 
>> initializer is then computed using the classic rules, which in this 
>> case give lub(String, int[]) = Serializable. So the type of 'var' is 
>> Serializable (all arrays have Serializable as superinterface, see JLS 
>> 4.10.3).
>
> I understood these rules, but hope it could be simplified in case of 
> "var".
>
> Just to clarify, Text like "INT#1","CAP#1", etc are standard elements 
> of the error messages, or a bugs?
These are normal features of javac diagnostics involving intersection 
types and captured types.

Maurizio
>
> public class testVar {
>
>     static class One implements Comparable, Serializable{
>
>         @Override
>         public int compareTo(Object o) {
>             return 0;
>         }
>     }
>
>     static class Two implements Comparable, Serializable{
>
>         @Override
>         public int compareTo(Object o) {
>             return 0;
>         }
>     }
>
>     public static void main(String[] args) {
>         var t = true ? new One(): new Two();
>         t[1]=0;
>         System.out.println("t.getClass() = " + t.getClass());
>     }
> }
>
> produce an error:
> testVar.java:41: error: array required, but INT#1 found
>         t[1]=0;
>          ^
>   where INT#1 is an intersection type:
>     INT#1 extends Object,Serializable,Comparable
> 1 error
>



More information about the compiler-dev mailing list