[英]Why are type variables for generics commonly declared as a single letter (e.g.: <T>)?
For every example in the TypeScript generics documentation and in most type definition files I've came across, type variables are declared as a single letter, usually <T>
. 对于TypeScript泛型文档中的每个示例以及我遇到的大多数类型定义文件,类型变量都声明为单个字母,通常为
<T>
。
Example: 例:
function identity<T>(arg: T): T {
return arg;
}
Questions: 问题:
The Java docs for Generics Types has a really good paragraph about this - and it applies to all languages that supports type variables: 泛型类型的Java文档有一个非常好的段落 - 它适用于所有支持类型变量的语言:
Type Parameter Naming Conventions
键入参数命名约定
By convention, type parameter names are single, uppercase letters.
按照惯例,类型参数名称是单个大写字母。 This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.
这与您已经了解的变量命名约定形成鲜明对比,并且有充分的理由:如果没有这种约定,就很难区分类型变量和普通类或接口名称。
The most commonly used type parameter names are:
最常用的类型参数名称是:
....
....
T - Type
T型
Basically you want to be able to quickly recognize that something is a type variable, and naming the type variable T
just became a standard code convention at some point. 基本上你希望能够快速识别某些东西是一个类型变量,并且命名类型变量
T
在某些时候才成为标准代码约定。
Microsoft's docs on C++ and C# has this convention too. 微软关于C ++和C#的文档也有这个约定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.