通用类型参数的 T、U、V 约定从何而来?
Where does the T, U, V convention for generic type params come from?
Java、C# 和 TypeScript(又名 Sun/Hejlsberg 语言家族)使用 T
、U
、V
等来表示泛型类型参数。从表面上看,这是因为 T
代表 "Type",而 U
和 V
在字母表中跟在 T
之后。
另一方面,Scala使用A
、B
、C
等,OCaml和Haskell使用a
、b
, 和 c
.
这些约定从何而来?是不是因为函数式语言更接近数学证明,其中 α
、β
、γ
是约定俗成的?
类似,但没有回答我的问题:Where does the C# generics naming convention come from?。
在标准 Java SE API 中,设计者通常选择与类型参数的含义/用途相关的单字母标识符:
Iterator<T>
- javadoc 其中 T
表示类型。其他示例是 ListIterator<T>
、Iterable<T>
、Comparable<T>
、Comparator<T>
和 Class<T>
。
Collection<E>
- javadoc 其中 E
表示元素。各种其他集合 类 和接口使用 E
.
Map<K,V>
- javadoc 其中 K
表示键,V
表示值。
Enum<E>
- javadoc 其中 E
表示枚举。
这些倾向于反驳您关于存在一般(广泛)T
、U
、V
约定的断言,至少对于 Java 而言。显然,在没有具体指导的情况下,一些个别设计人员会采用对指导的明显扩展来使用 T
(请参阅下面的链接),但这似乎是个人选择的结果。 (而且这些团体可能会认为这不值得讨论。)
(如果您想进行详尽搜索,请访问每个 javadocs 索引 A-Z 页面,并在其中搜索所有出现的“<”。)
I'm hoping for a link to an old discussion/commit/mailing list where the convention was first discussed.
在 Java 的情况下,我怀疑你会找到这个。讨论和邮件列表本来是私有的,并且 Java 当泛型被添加到语言中时,源代码仍然关闭,以及上面的所有示例。
@Lew Bloch 在 API 中找到了 T
、U
、V
的几个示例(见下文)添加到 Java Java 8 中的 SE 作为流支持的一部分。我断言这并不能证明一种普遍模式,大量先前存在的 类 反驳了它。
一般模式或惯例的其他负面证据:
- http://cr.openjdk.java.net/~alundblad/styleguide/index-v6.html#toc-type-variables 没有提到
U
和 V
.
- http://docs.oracle.com/javase/tutorial/java/generics/types.html 建议使用
S
、U
和 V
作为第二、第三和第四类参数。 (不是 U
、V
、W
...)
最后,JLS(JLS 6.1)推荐:
Type variable names should be pithy (single character if possible) yet evocative, and should not include lower case letters. This makes it easy to distinguish type parameters from ordinary classes and interfaces.
Container types should use the name E
for their element type. Maps should use K
for the type of their keys and V
for the type of their values. The name X
should be used for arbitrary exception types. We use T
for type, whenever there is not anything more specific about the type to distinguish it. (This is often the case in generic methods.)
If there are multiple type parameters that denote arbitrary types, one should use letters that neighbor T
in the alphabet, such as S
. Alternately, it is acceptable to use numeric subscripts (e.g., T1
, T2
) to distinguish among the different type variables. In such cases, all the variables with the same prefix should be subscripted.
简而言之,U
和V
在JLS中没有明确提及,但其他替代方案是。
Java、C# 和 TypeScript(又名 Sun/Hejlsberg 语言家族)使用 T
、U
、V
等来表示泛型类型参数。从表面上看,这是因为 T
代表 "Type",而 U
和 V
在字母表中跟在 T
之后。
另一方面,Scala使用A
、B
、C
等,OCaml和Haskell使用a
、b
, 和 c
.
这些约定从何而来?是不是因为函数式语言更接近数学证明,其中 α
、β
、γ
是约定俗成的?
类似,但没有回答我的问题:Where does the C# generics naming convention come from?。
在标准 Java SE API 中,设计者通常选择与类型参数的含义/用途相关的单字母标识符:
Iterator<T>
- javadoc 其中T
表示类型。其他示例是ListIterator<T>
、Iterable<T>
、Comparable<T>
、Comparator<T>
和Class<T>
。Collection<E>
- javadoc 其中E
表示元素。各种其他集合 类 和接口使用E
.Map<K,V>
- javadoc 其中K
表示键,V
表示值。Enum<E>
- javadoc 其中E
表示枚举。
这些倾向于反驳您关于存在一般(广泛)T
、U
、V
约定的断言,至少对于 Java 而言。显然,在没有具体指导的情况下,一些个别设计人员会采用对指导的明显扩展来使用 T
(请参阅下面的链接),但这似乎是个人选择的结果。 (而且这些团体可能会认为这不值得讨论。)
(如果您想进行详尽搜索,请访问每个 javadocs 索引 A-Z 页面,并在其中搜索所有出现的“<”。)
I'm hoping for a link to an old discussion/commit/mailing list where the convention was first discussed.
在 Java 的情况下,我怀疑你会找到这个。讨论和邮件列表本来是私有的,并且 Java 当泛型被添加到语言中时,源代码仍然关闭,以及上面的所有示例。
@Lew Bloch 在 API 中找到了 T
、U
、V
的几个示例(见下文)添加到 Java Java 8 中的 SE 作为流支持的一部分。我断言这并不能证明一种普遍模式,大量先前存在的 类 反驳了它。
一般模式或惯例的其他负面证据:
- http://cr.openjdk.java.net/~alundblad/styleguide/index-v6.html#toc-type-variables 没有提到
U
和V
. - http://docs.oracle.com/javase/tutorial/java/generics/types.html 建议使用
S
、U
和V
作为第二、第三和第四类参数。 (不是U
、V
、W
...)
最后,JLS(JLS 6.1)推荐:
Type variable names should be pithy (single character if possible) yet evocative, and should not include lower case letters. This makes it easy to distinguish type parameters from ordinary classes and interfaces.
Container types should use the name
E
for their element type. Maps should useK
for the type of their keys andV
for the type of their values. The nameX
should be used for arbitrary exception types. We useT
for type, whenever there is not anything more specific about the type to distinguish it. (This is often the case in generic methods.)If there are multiple type parameters that denote arbitrary types, one should use letters that neighbor
T
in the alphabet, such asS
. Alternately, it is acceptable to use numeric subscripts (e.g.,T1
,T2
) to distinguish among the different type variables. In such cases, all the variables with the same prefix should be subscripted.
简而言之,U
和V
在JLS中没有明确提及,但其他替代方案是。