Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
OrganizationEventAdvent CalendarQiitadon (β)
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.





Naming Type Parameters

In most cases, type parameters have descriptive names, such as Key and Value in Dictionary and Element in Array, which tells the reader about the relationship between the type parameter and the generic type or function it’s used in. However, when there isn’t a meaningful relationship between them, it’s traditional to name them using single letters such as T, U, and V, such as T in the swapTwoValues(::) function above.

ジェネリックスと出会ったのはJavaがはじめだったので、Javaの命名規約を見てみたところ、EはElementだと分かり、結局UやVには対して意味がなかったのも分かった、そしてsingle, uppercase letters だけでは意味が曖昧になり過ぎてダメじゃない?とも感じた。

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:

E - Element (used extensively by the Java Collections Framework)
K - Key
N - Number
T - Type
V - Value
S,U,V etc. - 2nd, 3rd, 4th types




/// An enum representing either a failure with an explanatory error, or a success with a result value.
public enum Result<T, Error: Swift.Error>: ResultProtocol, CustomStringConvertible, CustomDebugStringConvertible {
    case success(T)
    case failure(Error)


/// - Precondition: `a` and `b` do not alias each other.
public func swap<T>(_ a: inout T, _ b: inout T)  


  1. はじめからドキュメントを読んだらよかった
  2. 基本的に型との関係性を説明できる具体的な命名が良い。<K, V> よりも <Key, Value>
  3. 意味のないものはTなどにしておいた方が意味が大してないという意図が伝わるので良い
  4. 意味のないものが増えたらT,U,Vなど、Wが出てきたら設計大丈夫?見直そう。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Help us understand the problem. What are the problem?