Consider the following clause from the JLS (§15.9)
It is a compile-time error if a class instance creation expression provides type arguments to a constructor but uses the diamond form for type arguments to the class.
The reason why this is not allowed is given as the following
This rule is introduced because inference of a generic class's type arguments may influence the constraints on a generic constructor's type arguments.
Not very sure why this has been said so - consider the following typical usage of Type Parameters in the case of the generic class and the generic constructor:
class Scratch<T,R> {
public <K> Scratch(K k, T t, R r){
}
public static void main(String[] args) {
Scratch<Integer,Double> dd = new <String>Scratch<>("abdc",33,33.4); //compile error
}
}
Here we have the following:
- the Type parameter for the Constructor alone is K while for the class is {T,R}
- All these types are different from each other - and when the diamond operator is being used - the Class types cannot collide with the types being specified for the Constructor's type parameters.
Then how is the above reason for "not allowing the diamond operator when Type arguments for the Constructor are being provided" valid?. Am I missing something here?