@@ -752,6 +752,65 @@ a nonfailable initializer or an implicitly unwrapped failable initializer.
752
752
```
753
753
-->
754
754
755
+ ## Protocols that Have Only Semantic Requirements
756
+
757
+ All of the example protocols above require some methods or properties,
758
+ but a protocol declaration doesn't have to include any requirements.
759
+ You can also use a protocol to describe * semantic* requirements ---
760
+ that is, requirements about how values of those types behave
761
+ and about operations that they support.
762
+ <!--
763
+ Avoiding the term "marker protocol",
764
+ which more specifically refers to @_marker on a protocol.
765
+ -->
766
+ The Swift standard library defines several protocols
767
+ that don't have any required methods or properties:
768
+
769
+ - [ ` Sendable ` ] [ ] for values that can be shared across concurrency domains,
770
+ as discussed in < doc:Concurrency#Sendable-Types > .
771
+ - [ ` Copyable ` ] [ ] for values that Swift can copy
772
+ when you pass them to a function,
773
+ as discussed in < doc:Declarations#Borrowing-and-Consuming-Parameters > .
774
+ - [ ` BitwiseCopyable ` ] [ ] for values that can be copied, bit-by-bit.
775
+
776
+ [ `BitwiseCopyable` ] : https://developer.apple.com/documentation/swift/bitwisecopyable
777
+ [ `Copyable` ] : https://developer.apple.com/documentation/swift/copyable
778
+ [ `Sendable` ] : https://developer.apple.com/documentation/swift/sendable
779
+
780
+ <!--
781
+ These link definitions are also used in the section below,
782
+ Implicit Conformance to a Protocol.
783
+ -->
784
+
785
+ For information about these protocols' requirements,
786
+ see the overview in their documentation.
787
+
788
+ You use the same syntax to adopt these protocols
789
+ as you do to adopt other protocols.
790
+ The only difference is that you don't include
791
+ method or property declarations that implement the protocol's requirements.
792
+ For example:
793
+
794
+ ``` swift
795
+ struct MyStruct : Copyable {
796
+ var counter = 12
797
+ }
798
+
799
+ extension MyStruct : BitwiseCopyable { }
800
+ ```
801
+
802
+ The code above defines a new structure.
803
+ Because ` Copyable ` has only semantic requirements,
804
+ there isn't any code in the structure declaration to adopt the protocol.
805
+ Similarly, because ` BitwiseCopyable ` has only semantic requirements,
806
+ the extension that adopts that protocol has an empty body.
807
+
808
+ You usually don't need to write conformance to these protocols ---
809
+ instead, Swift implicitly adds the conformance for you,
810
+ as described in < doc:Protocols#Implicit-Conformance-to-a-Protocol > .
811
+
812
+ <!-- TODO: Mention why you might define your own empty protocols. -->
813
+
755
814
## Protocols as Types
756
815
757
816
Protocols don't actually implement any functionality themselves.
@@ -1368,6 +1427,96 @@ for level in levels.sorted() {
1368
1427
```
1369
1428
-->
1370
1429
1430
+ ## Implicit Conformance to a Protocol
1431
+
1432
+ Some protocols are so common that you would write them
1433
+ almost every time you declare a new type.
1434
+ For the following protocols,
1435
+ Swift automatically infers the conformance
1436
+ when you define a type that implements the protocol's requirements,
1437
+ so you don't have to write them yourself:
1438
+
1439
+ - [ ` Copyable ` ] [ ]
1440
+ - [ ` Sendable ` ] [ ]
1441
+ - [ ` BitwiseCopyable ` ] [ ]
1442
+
1443
+ <!--
1444
+ The definitions for the links in this list
1445
+ are in the section above, Protocols That Have Semantic Requirements.
1446
+ -->
1447
+
1448
+ You can still write the conformance explicitly,
1449
+ but it doesn't change how your code behaves.
1450
+ To suppress an implicit conformance,
1451
+ write a tilde (` ~ ` ) before the protocol name in the conformance list:
1452
+
1453
+ ``` swift
1454
+ struct FileDescriptor : ~ Sendable {
1455
+ let rawValue: Int
1456
+ }
1457
+ ```
1458
+
1459
+ <!--
1460
+ The example above is based on a Swift System API.
1461
+ https://github.com/apple/swift-system/blob/main/Sources/System/FileDescriptor.swift
1462
+
1463
+ See also this PR that adds Sendable conformance to FileDescriptor:
1464
+ https://github.com/apple/swift-system/pull/112
1465
+ -->
1466
+
1467
+ The code above shows part of a wrapper around POSIX file descriptors.
1468
+ The ` FileDescriptor ` structure
1469
+ satisfies all of the requirements of the ` Sendable ` protocol,
1470
+ which normally makes it sendable.
1471
+ However,
1472
+ writing ` ~Sendable ` suppresses this implicit conformance.
1473
+ Even though file descriptors use integers
1474
+ to identify and interact with open files,
1475
+ and integer values are sendable,
1476
+ making it nonsendable can help avoid certain kinds of bugs.
1477
+
1478
+ Another way to suppress implicit conformance
1479
+ is with an extension that you mark as unavailable:
1480
+
1481
+ ``` swift
1482
+ @available (* , unavailable )
1483
+ extension FileDescriptor Sendable { }
1484
+ ```
1485
+
1486
+ <!--
1487
+ - test: `suppressing-implied-sendable-conformance`
1488
+
1489
+ -> struct FileDescriptor {
1490
+ -> let rawValue: CInt
1491
+ -> }
1492
+
1493
+ -> @available(*, unavailable)
1494
+ -> extension FileDescriptor: Sendable { }
1495
+ >> let nonsendable: Sendable = FileDescriptor(rawValue: 10)
1496
+ !$ warning: conformance of 'FileDescriptor' to 'Sendable' is unavailable; this is an error in Swift 6
1497
+ !! let nonsendable: Sendable = FileDescriptor(rawValue: 10)
1498
+ !! ^
1499
+ !$ note: conformance of 'FileDescriptor' to 'Sendable' has been explicitly marked unavailable here
1500
+ !! extension FileDescriptor: Sendable { }
1501
+ !! ^
1502
+ -->
1503
+
1504
+ When you write ` ~Sendable ` in one place in your code,
1505
+ as in the previous example,
1506
+ code elsewhere in your program can still
1507
+ extend the ` FileDescriptor ` type to add ` Sendable ` conformance.
1508
+ In contrast,
1509
+ the unavailable extension in this example
1510
+ suppresses the implicit conformance to ` Sendable `
1511
+ and also prevents any extensions elsewhere in your code
1512
+ from adding ` Sendable ` conformance to the type.
1513
+
1514
+ > Note:
1515
+ > In addition to the protocols discussed above,
1516
+ > distributed actors implicitly conform to the [ ` Codable ` ] [ ] protocol.
1517
+
1518
+ [ `Codable` ] : https://developer.apple.com/documentation/swift/codable
1519
+
1371
1520
## Collections of Protocol Types
1372
1521
1373
1522
A protocol can be used as the type to be stored in
0 commit comments