what is sendable

Asked on 2024-07-30

6 searches

In Swift, a "sendable" type is one that can be safely shared between different concurrency domains, such as actors or threads, without causing data races. A sendable value protects its state from concurrent accesses, ensuring data race safety.

For example, a type might qualify as sendable if it acquires a lock while reading and writing mutable state. Swift requires explicit conformance to the Sendable protocol for public types to guarantee to clients that the type contains no mutable state that could lead to data races. This is particularly important for public types because it prevents premature locking of sendability, allowing for future changes without breaking guarantees.

Here are some relevant segments from the WWDC sessions:

  • In the session Migrate your app to Swift 6, it is explained that Swift does not infer sendability for public types automatically. You need to explicitly add sendable conformances on public types to avoid data race warnings.
  • The session A Swift Tour: Explore Swift’s features and design discusses how data race safety is achieved by requiring that values shared between concurrency domains are sendable.

Relevant Sessions

  1. Migrate your app to Swift 6
  2. A Swift Tour: Explore Swift’s features and design