|
1 | 1 | # プロトコル\(Protocols\) |
2 | 2 |
|
3 | | -最終更新日: 2024/3/18 |
| 3 | +最終更新日: 2025/5/24 |
4 | 4 | 原文: https://docs.swift.org/swift-book/LanguageGuide/Protocols.html |
5 | 5 |
|
6 | 6 | 準拠型が実装する必要がある要件を定義する。 |
@@ -248,6 +248,32 @@ class SomeSubClass: SomeSuperClass, SomeProtocol { |
248 | 248 |
|
249 | 249 | 失敗可能イニシャライザ要件は、準拠する型の失敗可能または失敗しないイニシャライザによって満たされます。失敗しないイニシャライザ要件は、失敗しないイニシャライザまたは暗黙的にアンラップされた失敗可能イニシャライザによって満たされます。 |
250 | 250 |
|
| 251 | +## セマンティック要件のみを持つプロトコル\(Protocols that Have Only Semantic Requirements\) |
| 252 | + |
| 253 | +上記のすべてのプロトコル例では、いくつかのメソッドやプロパティが必要ですが、プロトコル宣言に要件を含める必要はありません。プロトコルを使用して、セマンティック要件、つまりそれらの型の値がどのように動作するか、およびそれらがサポートする操作に関する要件を記述することもできます。 |
| 254 | + |
| 255 | +Swift 標準ライブラリは、必須のメソッドやプロパティを持たないいくつかのプロトコルを定義しています。 |
| 256 | + |
| 257 | +- [`Sendable`](https://developer.apple.com/documentation/swift/sendable): 並行処理ドメイン間で共有できる値用([`Sendable` 型\(Sendable Types\)](concurrency.md#sendable-型sendable-types)で説明) |
| 258 | +- [`Copyable`](https://developer.apple.com/documentation/swift/copyable): 関数に渡すときに Swift がコピーできる値用([Borrowing と Consuming パラメータ\(Borrowing and Consuming Parameters\)](../language-reference/declarations.md#borrowing-と-consuming-パラメータborrowing-and-consuming-parameters)で説明) |
| 259 | +- [`BitwiseCopyable`](https://developer.apple.com/documentation/swift/bitwisecopyable):ビット単位でコピーできる値用 |
| 260 | + |
| 261 | +これらのプロトコルの要件に関する情報については、それぞれのドキュメントの概要を参照してください。 |
| 262 | + |
| 263 | +これらのプロトコルを採用する場合も、他のプロトコルを採用する場合と同じ構文を使用します。唯一の違いは、プロトコルの要件を実装するメソッドやプロパティの宣言を含めないことです。次に例を示します。 |
| 264 | + |
| 265 | +```swift |
| 266 | +struct MyStruct: Copyable { |
| 267 | + var counter = 12 |
| 268 | +} |
| 269 | + |
| 270 | +extension MyStruct: BitwiseCopyable { } |
| 271 | +``` |
| 272 | + |
| 273 | +上記のコードは新しい構造体を定義しています。`Copyable` にはセマンティック要件しかないため、構造体宣言にはプロトコルを採用するためのコードはありません。同様に、`BitwiseCopyable` にはセマンティック要件しかないため、そのプロトコルを採用する extension の本体は空です。 |
| 274 | + |
| 275 | +通常、これらのプロトコルへの準拠を記述する必要はありません。代わりに、[プロトコルへの暗黙の準拠\(Implicit Conformance to a Protocol\)](#プロトコルへの暗黙の準拠implicit-conformance-to-a-protocol)で説明されているように、Swift が暗黙的に準拠を追加します。 |
| 276 | + |
251 | 277 | ## <a id="protocols-as-types">型としてのプロトコル\(Protocols as Types\)</a> |
252 | 278 |
|
253 | 279 | プロトコルは、実際には機能を実装しません。 |
@@ -512,6 +538,35 @@ for level in levels.sorted() { |
512 | 538 | // expert(stars: 5) |
513 | 539 | ``` |
514 | 540 |
|
| 541 | +## <a id="implicit-conformance-to-a-protocol">プロトコルへの暗黙の準拠\(Implicit Conformance to a Protocol\)</a> |
| 542 | + |
| 543 | +一部のプロトコルは非常によく使われるため、ほとんどの場合、新しい型を宣言するたびにそれらを記述することになるでしょう。以下のプロトコルについては、プロトコルの要件を実装する型を定義すると、Swift が自動的に準拠を推論するため、自分で記述する必要はありません。 |
| 544 | + |
| 545 | +- [`Copyable`](https://developer.apple.com/documentation/swift/copyable) |
| 546 | +- [`Sendable`](https://developer.apple.com/documentation/swift/sendable) |
| 547 | +- [`BitwiseCopyable`](https://developer.apple.com/documentation/swift/bitwisecopyable) |
| 548 | + |
| 549 | +明示的に準拠を記述することもできますが、それによってコードの動作が変わることはありません。暗黙的な準拠を抑制するには、準拠リストのプロトコル名の前にチルダ(`~`)を記述します。 |
| 550 | + |
| 551 | +```swift |
| 552 | +struct FileDescriptor: ~Sendable { |
| 553 | + let rawValue: Int |
| 554 | +} |
| 555 | +``` |
| 556 | + |
| 557 | +上記のコードは、POSIX ファイルディスクリプタのラッパーの一部を示しています。`FileDescriptor` 構造体は `Sendable` プロトコルのすべての要件を満たしており、通常はこれにより `Sendable` になります。しかし、`~Sendable` と記述することで、この暗黙的な準拠が抑制されます。ファイルディスクリプタは開いているファイルを識別し操作するために整数を使用し、整数値は `Sendable` ですが、これを非 `Sendable` にすることで、特定種類のバグを回避するのに役立ちます。 |
| 558 | + |
| 559 | +暗黙的な準拠を抑制するもう 1 つの方法は、利用不可としてマークした extension を使用することです。 |
| 560 | + |
| 561 | +```swift |
| 562 | +@available(*, unavailable) |
| 563 | +extension FileDescriptor Sendable { } |
| 564 | +``` |
| 565 | + |
| 566 | +前の例のように、コードのある場所で `~Sendable` と記述した場合でも、プログラムの他の場所のコードは `FileDescriptor` 型を拡張して `Sendable` 準拠を追加できます。対照的に、この例の `unavailable` extension は、`Sendable` への暗黙的な準拠を抑制し、さらにコードの他の場所にある extension がその型に `Sendable` 準拠を追加するのを防ぎます。 |
| 567 | + |
| 568 | +> 注:上記のプロトコルに加えて、分散(distributed)アクターは、暗黙的に [`Codable`](https://developer.apple.com/documentation/swift/codable) プロトコルに準拠します。 |
| 569 | +
|
515 | 570 | ## プロトコル型のコレクション\(Collections of Protocol Types\) |
516 | 571 |
|
517 | 572 | プロトコルは、[Protocols as Types\(型としてのプロトコル\)](protocols.md#protocols-as-types)で説明されているように、配列や辞書などのコレクションに格納される型として使用できます。この例では、`TextRepresentable` の配列を作成します。 |
|
0 commit comments