Fixing §4.4, type variables
Wolf Lammen
ookami1 at gmx.de
Wed Jun 15 13:48:21 UTC 2022
Hi,
I think, §4.4 needs a review. It contains a questionable definiton, and is sometimes imprecise in its wording. This comment pertains to the Java Language Specification 8 and 9. I try to use the wording of the standard as much as possible. Such words are quoted in apostrophes, and are usually accompanied by a text reference to a relevant JLS9 section in parenthesis on first use.
I presume, the idea of a 'type variable' is that of a compile-time variable (as opposed to a symbol with fixed semantics) taking on types, and being updated on instantiations of generic entities. At least, this springs to my mind when I read these words in the specification. This is how it is defined in the first sentence of section §4.4:
"A type variable is an unqualified identifier used as a type in class, interface, method, and constructor bodies."
This is awkward in several respects.
1. The following example shows a particular shortcoming of this definition:
class X
<T1, T2 extends T1 > // 'type parameter section' (§8.1.2)
{ T2 x; } // 'class body' (§8.6.1)
The second occurrence of T1 is syntactically a 'nonterminal symbol' (§2.1) 'TypeVariable' (§4.3). However, its 'Identifier' (§3.8) "T1" does not appear in the 'ClassBody' (§8.6.1), so according to the definition above, it is formally not a 'type variable' (§4.4). I cannot see a reason, why "T2" is a 'type variable.' but "T1" not.
2. The 'Identifier' (§3.8) production rule allows no punctuation characters, hence an 'identifier' (§3.8) is never qualified. 'Names' (§6.2) can be qualified. This definition's 'identifier' is missing from the enumeration of identifier usage in §6.2.
3. Throughout the document, 'type variable' is used as if it not just an 'identifier'. It is likely meant to be the program entity introduced by the 'type parameter' 'declaration' (§6.2), as expressed in the second sentence of §4.4.
As an example for the confusion this causes, look at another sentence in §4.4:
"The scope of a type variable declared as a type parameter is specified in §6.3."
A 'type variable' has no scope, since it is just an 'identifier'. There is no 'type variable' without a matching declaration in a 'type parameter', this is mandatory. It is the scope of a 'type parameter' that is meant here and is explained in §6.3. It seems the author felt the need to connect this scope to the notion of 'type variable' for better understanding.
I suggest to put the second sentence of §4.4 to the front, and continue along the lines "A type variable appears elsewhere as an unqualified identifier used as a type within a generic class, interface, method, or constructor.", or similar. Adjust §6.2 to cover type variable identifiers.
Regards
Wolf Lammen
More information about the jls-jvms-spec-comments
mailing list