Reputation: 875
In section 4.5 of JLS (Parameterized Types) is mentioned the following notation:
S[F1:=T1,...,Fn:=Tn]
.
I do not exactly unbderstand the meaning of this notation.
It is mentioned in the following paragraph:
A generic type has type parameters F1,...,Fn with corresponding bounds B1,...,Bn. Each type argument Ti of a parameterized type ranges over all types that are subtypes of all types listed in the corresponding bound. That is, for each bound type S in Bi, Ti is a subtype of S[F1:=T1,...,Fn:=Tn] (§4.10).
The meaning of the statement above is clear.
But it is not clear for me why we have [F1:=T1,...,Fn:=Tn]
after S
Upvotes: 3
Views: 206
Reputation: 924
§1.3 defines the meaning of the notation:
The type system of the Java programming language occasionally relies on the notion of a substitution. The notation [F_1:=T1,...,Fn:=Tn] denotes substitution of Fi by Ti for 1 ≤ i ≤ n.
Let's not forget that an interface type S in every bound can be generic itself. That is why depending on the substitution, the resulting type S can be different.
For example,
interface SomeInterface<X, Y> { }
class Gen<T, Z extends Number & SomeInterface<T, Z>> {}
Upvotes: 0
Reputation: 11037
It just looks wrong to me.
First: without the bracketed part, the sentence is a correct restating of the previous statement, and both match my understanding of the java language.
Second: the notation is wrong. §4.10 references the subtyping and supertyping notation <: and :>, but what is actually used is the replacement notation := (not actually defined as replacement notation in version 8 but was in version 7, and used as replacement in other sections of the document).
Finally: The previous version of the JSL had an even more nonsensical version of the same notation.
A generic class or interface declaration C (§8.1.2, §9.1.2) with one or more type parameters A1,...,An which have corresponding bounds B1,...,Bn defines a set of parameterized types, one for each possible invocation of the type parameter section.
Each parameterized type in the set is of the form C where each type argument Ti ranges over all types that are subtypes of all types listed in the corresponding bound. That is, for each bound type Si in Bi, Ti is a subtype of Si[F1:=T1,...,Fn:=Tn].
It has the same brackets, but this time does not even mention what F is prior to being used in the brackets.
It looks like the brackets were copied incorrectly when this section was rewritten between versions 6 and 7. Version 6 has the brackets but the simply mean the bounds check happens for each type parameter.
Upvotes: -1
Reputation: 875
Here ia my thinking.
The notation [F1:=T1,...,Fn:=Tn]
is a substitution of each type parameter Fi
with concete type (or wildcard) Ti, i=1,2,...n
.
Generic class can be thought as a function with (n+1)
arguments f(F1,F2,...,Fn, S)
, where Fi
- is a type parameter and S
belong to the following finite set {B1,B2,...,Bn}
.
If we substitute [F1:=T1,...,Fn:=Tn]
we get function with one argument:
g(s)=f(T1,T2,...,Tn,S)
.
Considering that [F1:=T1,...,Fn:=Tn]
is a substitution of Fi
with Ti, i=1,2,...,n
we can introduce new notation:
f(T1,T2,...,Tn,S)=h(S)[F1=T1,...,Fn=Tn]
.
Function h(S)
should be linear: h(S)=S
Finally we have:
f(T1,T2,...,Tn,S)=S[F1=T1,...,Fn=Tn]
.
Hence notion S[F1=T1,...,Fn=Tn]
means: get one of upper bound Bi
in fixed values Fi=Ti
.
Upvotes: 0