what is sendable type?

Asked on 08/05/2024

1 search

A sendable type in Swift is a type that can be safely transferred between different concurrency domains, such as actors or threads, without causing data races. This is crucial for ensuring data-race safety in concurrent programming.

In Swift, a type is considered sendable if it protects its state from concurrent accesses. 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 is safe to be sent across concurrency boundaries.

Here are some key points about sendable types from the context:

  • Public Types: Swift does not infer sendability for public types automatically. You need to explicitly mark them as sendable to avoid locking in sendability prematurely (Migrate your app to Swift 6).
  • Data-Race Safety: In Swift 6, the data-race safety of your program is fully verified at compile time. One way to achieve this is by requiring that values shared between concurrency domains are sendable (A Swift Tour: Explore Swift’s features and design).

For more detailed information, you can refer to the session Migrate your app to Swift 6 and A Swift Tour: Explore Swift’s features and design.