Generic Parameters and Arguments 総称体パラメータと引数

This chapter describes parameters and arguments for generic types, functions, and initializers. When you declare a generic type, function, subscript, or initializer, you specify the type parameters that the generic type, function, or initializer can work with. These type parameters act as placeholders that are replaced by actual concrete type arguments when an instance of a generic type is created or a generic function or initializer is called. この章は、総称体の型、関数、そしてイニシャライザに対するパラメータと引数を記載します。あなたがある総称体型、添え字、関数、またはイニシャライザを宣言するとき、あなたはその総称体型、関数、またはイニシャライザが扱うことができる型パラメータを指定します。これらの型パラメータは、総称体型のインスタンスがつくられる、あるいは、総称体の関数やイニシャライザが呼ばれるとき、実際の具象型引数と取り替えられるプレースホルダの働きをします。

For an overview of generics in Swift, see Generics. スウィフトでの総称体の概要のために、総称体を見てください。

Generic Parameter Clause 総称体パラメータ節

A generic parameter clause specifies the type parameters of a generic type or function, along with any associated constraints and requirements on those parameters. A generic parameter clause is enclosed in angle brackets (<>) and has the following form: 総称体パラメータ節は、総称体の型や関数の型パラメータを、それらのパラメータに関するあらゆる付随する制約と要件と一緒に指定します。総称体パラメータ節は、山形括弧(<>)において囲まれて、以下の書式を持ちます:

  1. <generic parameter list>

The generic parameter list is a comma-separated list of generic parameters, each of which has the following form: 総称体パラメータリストは、コンマで区切られた総称体パラメータのリストです、そしてそのそれぞれは以下の形式を持ちます:

  1. type parameter: constraint

A generic parameter consists of a type parameter followed by an optional constraint. A type parameter is simply the name of a placeholder type (for example, T, U, V, Key, Value, and so on). You have access to the type parameters (and any of their associated types) in the rest of the type, function, or initializer declaration, including in the signature of the function or initializer. 総称体パラメータは、型パラメータとそれに続く任意の制約から成ります。型パラメータは、単にプレースホルダ型の名前です(たとえばTUVKeyValueなど)。あなたは、そのままの型、関数、またはイニシャライザ宣言の中の、さらに含めて関数またはイニシャライザのシグネチャの中の、型パラメータ(およびあらゆるそれの関連型)にアクセスします。

The constraint specifies that a type parameter inherits from a specific class or conforms to a protocol or protocol composition. For example, in the generic function below, the generic parameter T: Comparable indicates that any type argument substituted for the type parameter T must conform to the Comparable protocol. 制約は、型パラメータが特定のクラスから継承する、もしくはあるプロトコルまたはプロトコル合成に準拠することを指定します。たとえば、下記の総称体関数において、総称体パラメータT: Comparableは、型パラメータTと置き換えられる任意の型の引数はComparableプロトコルに準拠しなければならないことを示します。

  1. func simpleMax<T: Comparable>(_ x: T, _ y: T) -> T {
  2. if x < y {
  3. return y
  4. }
  5. return x
  6. }

Because Int and Double, for example, both conform to the Comparable protocol, this function accepts arguments of either type. In contrast with generic types, you don’t specify a generic argument clause when you use a generic function or initializer. The type arguments are instead inferred from the type of the arguments passed to the function or initializer. 例えば、IntDoubleは両方ともComparableプロトコルに準拠するので、この関数はどちらの型の引数でも受け入れます。総称体型と対照的に、あなたが総称体の関数やイニシャライザを使うとき、あなたは総称体引数節を指定しません。型引数は、その代わりに関数またはイニシャライザに渡される引数の型から推論されます。

  1. simpleMax(17, 42) // T is inferred to be Int(Tは、Intであると推論されます)
  2. simpleMax(3.14159, 2.71828) // T is inferred to be Double(TはDoubleであると推論されます)

Generic Where Clauses 総称体where節

You can specify additional requirements on type parameters and their associated types by including a generic where clause right before the opening curly brace of a type or function’s body. A generic where clause consists of the where keyword, followed by a comma-separated list of one or more requirements. あなたは、追加の要件を型パラメータおよびそれの関連型に指定することが総称体where節を型または関数の本文の開き波括弧のまさに前に含めることによって行えます。総称体where節は、whereキーワード、それに続けてコンマ区切りのリストのひとつ以上の要件(requirements)から構成されます。

  1. where requirements

The requirements in a generic where clause specify that a type parameter inherits from a class or conforms to a protocol or protocol composition. Although the generic where clause provides syntactic sugar for expressing simple constraints on type parameters (for example, <T: Comparable> is equivalent to <T> where T: Comparable and so on), you can use it to provide more complex constraints on type parameters and their associated types. For example, you can constrain the associated types of type parameters to conform to protocols. For example, <S: Sequence> where S.Iterator.Element: Equatable specifies that S conforms to the Sequence protocol and that the associated type S.Iterator.Element conforms to the Equatable protocol. This constraint ensures that each element of the sequence is equatable. 総称体where節の中の要件が指定するのは、型パラメータがクラスから継承するかまたはプロトコルやプロトコル合成に準拠するということです。総称体where節は、型パラメータ上で単純な制約を表すために糖衣構文を提供しますが(例えば、<T: Comparable><T> where T: Comparableに等しいなどなど)、あなたはそれを使ってより複雑な制約を型パラメータとそれの関連型に提供することができます。たとえば、あなたは型パラメータの関連型をいくらかのプロトコルに準拠するように制約することができます。例えば、<S: Sequence> where S.Iterator.Element: Equatableが指定するのは、SSequenceプロトコルに準拠すること、そして関連型S.Iterator.ElementEquatableプロトコルに準拠することです。この制約はシーケンスの各要素が同等比較可能であることを保証します。

You can also specify the requirement that two types be identical, using the == operator. For example, <S1: Sequence, S2: Sequence> where S1.Iterator.Element == S2.Iterator.Element expresses the constraints that S1 and S2 conform to the Sequence protocol and that the elements of both sequences must be of the same type. あなたはまた、==演算子を使用して、2つの型が同一であるという要件を指定することができます。例えば、<S1: Sequence, S2: Sequence> where S1.Iterator.Element == S2.Iterator.Elementが表す制約は、S1S2Sequenceプロトコルに準拠すること、そして両方のシーケンスの要素が同じ型でなければならないということです。

Any type argument substituted for a type parameter must meet all the constraints and requirements placed on the type parameter. 型パラメータと置き換えられるどんな型引数でも、型パラメータに設置された制約と要件の全てに応じなければなりません。

A generic where clause can appear as part of a declaration that includes type parameters, or as part of a declaration that’s nested inside of a declaration that includes type parameters. The generic where clause for a nested declaration can still refer to the type parameters of the enclosing declaration; however, the requirements from that where clause apply only to the declaration where it’s written. 総称体where節は、型パラメータを含む宣言の一部として、または型パラメータを含む宣言の内部に入れ子にされる宣言の一部として現れることができます。入れ子にされた宣言に対する総称体where節は、囲んでいる宣言の型パラメータを依然として参照できます;しかしながら、そのwhere節からの要件は、それが書かれているところの宣言に対してのみ適用されます。

If the enclosing declaration also has a where clause, the requirements from both clauses are combined. In the example below, startsWithZero() is available only if Element conforms to both SomeProtocol and Numeric. 囲んでいる宣言がまたwhere節を持つならば、両方の節からの要件は結合されます。下の例において、startsWithZero()は、ElementSomeProtocolNumericの両方に準拠する場合にのみ利用可能です。

  1. extension Collection where Element: SomeProtocol {
  2. func startsWithZero() -> Bool where Element: Numeric {
  3. return first == .zero
  4. }
  5. }

You can overload a generic function or initializer by providing different constraints, requirements, or both on the type parameters. When you call an overloaded generic function or initializer, the compiler uses these constraints to resolve which overloaded function or initializer to invoke. あなたは、総称体の関数やイニシャライザをオーバーロードすることが、異なる制約、要件、または両方を型パラメータ上で提供することによって可能です。あなたがオーバーロードされた総称体関数またはイニシャライザを呼ぶとき、コンパイラはこれらの制約を使って、呼び出すことになるオーバーロードされた関数またはイニシャライザはどれか決定します。

For more information about generic where clauses and to see an example of one in a generic function declaration, see Generic Where Clauses. 総称体where節のさらなる情報のために、そして総称体関数宣言におけるそれの一例を見るために、総称体where節を見てください。

Grammar of a generic parameter clause 総称体パラメータ節の文法

generic-parameter-clause < generic-parameter-list >

generic-parameter-list generic-parameter | generic-parameter , generic-parameter-list

generic-parameter type-name

generic-parameter type-name : type-identifier

generic-parameter type-name : protocol-composition-type

generic-where-clause where requirement-list

requirement-list requirement | requirement , requirement-list

requirement conformance-requirement | same-type-requirement

conformance-requirement type-identifier : type-identifier

conformance-requirement type-identifier : protocol-composition-type

same-type-requirement type-identifier == type

Generic Argument Clause 総称体引数節

A generic argument clause specifies the type arguments of a generic type. A generic argument clause is enclosed in angle brackets (<>) and has the following form: 総称体引数節は、総称体型の型引数を指定します。総称体引数節は、山形括弧(<>)に囲まれて、以下の形式を持ちます:

  1. <generic argument list>

The generic argument list is a comma-separated list of type arguments. A type argument is the name of an actual concrete type that replaces a corresponding type parameter in the generic parameter clause of a generic type. The result is a specialized version of that generic type. The example below shows a simplified version of the Swift standard library’s generic dictionary type. 総称体引数リストは、型引数のコンマで区切られたリストです。型引数は実際の具象型の名前です、それは、ある総称体型のもつ総称体パラメータ節の中の対応する型パラメータを置き換えます。結果は、その総称体型の特殊化版です。下の例は、スウィフト標準ライブラリの総称体辞書型の簡略版を示します。

  1. struct Dictionary<Key: Hashable, Value>: Collection, ExpressibleByDictionaryLiteral {
  2. /* ... */
  3. }

The specialized version of the generic Dictionary type, Dictionary<String, Int> is formed by replacing the generic parameters Key: Hashable and Value with the concrete type arguments String and Int. Each type argument must satisfy all the constraints of the generic parameter it replaces, including any additional requirements specified in a generic where clause. In the example above, the Key type parameter is constrained to conform to the Hashable protocol and therefore String must also conform to the Hashable protocol. 総称体Dictionary型の特殊化版、Dictionary<String, Int>は、総称体パラメータKey: HashableおよびValueを具象型引数StringおよびIntと置き換えて作り上げられます。各型引数は、それが置き換える総称体パラメータの全ての制約を、総称体where節で指定される任意の追加の要件を含めて、満たさなければなりません。上の例で、Key型パラメータは、Hashableプロトコルに準拠することを強制されます、したがってStringもまたHashableプロトコルに準拠しなければなりません。

You can also replace a type parameter with a type argument that’s itself a specialized version of a generic type (provided it satisfies the appropriate constraints and requirements). For example, you can replace the type parameter Element in Array<Element> with a specialized version of an array, Array<Int>, to form an array whose elements are themselves arrays of integers. あなたはまた、型パラメータを、それ自身が総称体型の特殊化版である型引数と置き換えることができます(それが適切な制約と要件を満たすという条件で)。例えば、あなたはArray<Element>における型パラメータElementを配列の特殊化版、Array<Int>で置き換えて、要素それ自身が整数の配列である配列を作り出すことができます。

  1. let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

As mentioned in Generic Parameter Clause, you don’t use a generic argument clause to specify the type arguments of a generic function or initializer. 総称体パラメータ節で言及されるように、あなたは、総称体の関数やイニシャライザの型引数を指定するために総称体引数節を使いません。

Grammar of a generic argument clause 総称体引数節の文法

generic-argument-clause < generic-argument-list >

generic-argument-list generic-argument | generic-argument , generic-argument-list

generic-argument type

Patterns パターン

Summary of the Grammar 文法の概要