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

Sergey Bylokhov Sergey.Bylokhov at oracle.com
Tue Sep 19 16:58:45 UTC 2017


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?

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

-- 
Best regards, Sergey.


More information about the compiler-dev mailing list