泛型类型参数的T,U,V约定从何而来? [英] Where does the T, U, V convention for generic type params come from?

查看:291
本文介绍了泛型类型参数的T,U,V约定从何而来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java,C#和TypeScript(又名Sun/Hejlsberg语言家族)使用TUV等表示通用类型参数.表面上这是因为T代表类型",而UV在字母中紧跟T.

Java, C#, and TypeScript (aka. the Sun/Hejlsberg family of languages) use T, U, V, etc. to represent generic type parameters. Ostensibly that's because T stands for "Type", and U and V follow T in the alphabet.

另一方面,Scala使用ABC等,而OCaml和Haskell使用abc.

On the other hand, Scala uses A, B, C and so on, and OCaml and Haskell use a, b, and c.

这些约定从何而来?是因为功能语言更接近于数学证明,而习惯上使用αβγ呢?

Where do these conventions come from? Is it because the functional languages are closer to math proofs, where α, β, and γ are used by convention?

类似,但没有回答我的问题: C#泛型命名约定从何而来?.

Similar, but doesn't answer my question: Where does the C# generics naming convention come from?.

推荐答案

在标准Java SE API中,设计人员通常选择一个与类型参数的含义/目的有关的单字母标识符:

In the standard Java SE API's, the designer have typically chosen a one-letter identifier that is related to the meaning / purpose of the type parameter:

  • 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表示枚举.
  • Iterator<T> - javadoc where T means type. Other examples are ListIterator<T>, Iterable<T>, Comparable<T>, Comparator<T> and Class<T>.
  • Collection<E> - javadoc where E means element. Various other collection classes and interfaces use E.
  • Map<K,V> - javadoc where K means key, and V means value.
  • Enum<E> - javadoc where E means enum.

这些倾向反驳您的断言,即至少对于Java,存在通用(广泛使用)的TUV约定.显然,在没有具体指导的情况下,一些个体设计师将采用明显的指导扩展来使用T(请参见下面的链接),但这似乎是个体选择的结果. (而且这些团体可能会认为这不值得讨论.)

These tend to disprove your assertion that there is a general (widespread) T, U, V convention, at least for Java. Obviously, in the absence of specific guidance, some individual designers will adopt what is an obvious extension to the guidance to use T (see links below) but that would appear to be a result of individual choice. (And the groups probably would have treated this as not worthy of discussion.)

(如果要进行详尽的搜索,请访问每个javadocs索引的A-Z页,并在其中搜索所有出现的<".)

(If you want to do an exhaustive search, visit each of the javadocs index A-Z pages, and search them for all occurrences of "<".)

我希望链接到最初讨论该约定的旧讨论/提交/邮件列表.

I'm hoping for a link to an old discussion/commit/mailing list where the convention was first discussed.

对于Java,我怀疑您会发现这一点.讨论和邮件列表本来是私有的,当将泛型以及上述所有示例添加到Java语言时,Java源代码仍然关闭.

In the case of Java, I doubt that you will find this. The discussions and mailing lists would have been private, and the Java source code was still closed when generics were added to the language, along with all of the examples above.

@Lew Bloch在Java 8中作为流支持的一部分而添加到Java SE的API中,找到了TUV的一些示例(见下文).我断言这并不能证明是一种通用模式,并且大量先前存在的类都不能证明这一点.

@Lew Bloch has found a few example (see below) of T, U, V in the APIs added to Java SE in Java 8 as part of the streams support. I assert that this does not prove a general pattern, and the large number of preexisting classes disprove it.

关于一般模式或惯例的其他负面证据:

Other negative evidence of a general pattern or convention:

  • http://cr.openjdk.java.net/~alundblad/styleguide/index-v6.html#toc-type-variables which doesn't mention U and V.
  • http://docs.oracle.com/javase/tutorial/java/generics/types.html which recommends use of S, U and V as 2nd, 3rd and 4th type parameters. (Not U, V, W ...)

最后,JLS( JLS 6.1 )建议:

Finally, the JLS (JLS 6.1) recommends:

类型变量名称应精简(如果可能,应使用单个字符)但应引起人们的注意,并且不应包含小写字母.这样可以轻松地将类型参数与普通的类和接口区分开.

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.

容器类型的元素类型应使用名称E.映射的键类型应使用K,其值的类型应使用V.名称X应该用于任意异常类型.每当类型没有更具体的区别时,我们就使用T作为类型. (在通用方法中通常是这种情况.)

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.)

如果有多个表示任意类型的类型参数,则应该使用字母中与T相邻的字母,例如S.或者,可以接受使用数字下标(例如T1T2),以区分不同类型的变量.在这种情况下,所有带相同前缀的变量都应加下标.

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.

简而言之,JLS中没有明确提及UV,但其他替代方法是

In short, U and V are not explicitly mentioned in the JLS, but others alternatives are.

这篇关于泛型类型参数的T,U,V约定从何而来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆