Java 8 type inference - How is abbreviation done for generic constructors?

I read the output of the java 8 language specification type. It says that

List<String> ls = new ArrayList<>() 

will be reduced first

 ArrayList<α> -> List<String> 

and then

 α <= String 

and in the end

 α = String 

It’s hard for me to understand how reducing the limit

 ArrayList<α> -> List<String> to α <= String 

. It would be very helpful if anyone could point to the logic using the java 8 language specification.

Below is a link to abbreviation


Thanks #Holger for the explanation. The following is my conclusion about the conclusion

 new ArrayList<> -> List<String> to ArrayList<α> -> List<String> 

Please correct me if I am wrong.

First, to find a temporary method for the constructor, we use # 15.9.3

  • Otherwise, the constructor arguments are arguments in the argument list of the class instance creation expression, if any, in the order they appear in the expression.

  • If the class instance creation expression uses <> for the class class elide arguments, the list of methods m1 ... mn is defined for purposes of overload resolution and type argument output.

Then # 18.5.2 is used to output

 ArrayList<α> -> List<String> 

Due to the fact that it is a political expression and does not have any parameters such as a wild card;

  • Otherwise, the restriction formula is reduced and is included in B2.
+6
source share
1 answer

Im glad you didn’t ask how to come from new ArrayList<>() → List<String> to ArrayList<α> → List<String> , since, looking obvious, for applying §18.2.1 we find that it requires discussion of the whole of §15.9.3, followed by a discussion of §18.5.2.

Based on ArrayList<α> → List<String> it's easier:

§18.2.2. Type Compatibility Restrictions

The form restriction formula is as follows:

  • ... (five inapplicable bullets omitted)

  • Otherwise, the restriction reduces to .

§18.2.3. Subtype Limitations

The form restriction formula is as follows:

  • ... (five unnecessary bullets omitted)

  • Otherwise, the restriction is reduced in accordance with the form T :

    • If T is a parameterized class or interface type or an internal class type of a parameterized class or interface type (directly or indirectly), let A 1 , ..., A n be arguments of type T Among the supertypes S , the corresponding class or interface type is identified with arguments of type B 1 , ..., B n . If this type does not exist, the restriction is reduced to false. Otherwise, the restriction reduces to the following new restrictions: for all I (1 ≤ i ≤ n), i <= A i > ..

So, ArrayList<α> → List<String> first comes down to ArrayList<α> <: List<String> , then, since List<String> is a parameterized interface, the corresponding super type ArrayList<α> identified, which will be List<α> , then the restriction decreases to the set of constraints for each corresponding type parameter (here we have exactly one), therefore we get α <= String .

+4
source

All Articles