美文网首页
Naming Type Parameter

Naming Type Parameter

作者: 幸运的小强本人 | 来源:发表于2016-03-07 07:21 被阅读5次

In simple cases where a generic function or generic type refers to a single placeholder type (such as the swapTwoValues generic function above, or a generic collection that stores a single type, such as Array), it is traditional to use the single-character name T for the type parameter. However, you can use any valid identifier as the type parameter name.

If you are defining more complex generic functions, or generic types with multiple parameters, it is useful to provide more descriptive type parameter names. For example, Swift's Dictionary type has two type parameters - one for its keys and one for its values. If you were writing Dictionary yourself, you might name these two type parameter Key and Value to remind you of their purpose as you use them within your generic code.

Always give type parameters UpperCamelCase names (such as T and Key) to indicate that they are a placeholder for a type, not a value.

相关文章

网友评论

      本文标题:Naming Type Parameter

      本文链接:https://www.haomeiwen.com/subject/pcbakttx.html