when to use @Sendable?

Asked on 09/06/2024

1 search

In Swift, the @Sendable attribute is used to mark types or functions as safe to be used across concurrency domains, ensuring that they do not lead to data races. This is particularly important when dealing with public types, as Swift does not automatically infer sendability for them. By marking a type as Sendable, you are guaranteeing to your clients that the type contains no mutable state that could lead to concurrency issues.

For example, in the session "Migrate your app to Swift 6," it is explained that if you have a public type that you want to share across different actors, you need to explicitly mark it as Sendable. This is because Swift requires explicit sendable conformances on public types to avoid locking in sendability prematurely, which could be problematic if the type's internal state changes in the future.

If you want to learn more about this topic, you can refer to the session Migrate your app to Swift 6 (32:27) where this concept is discussed in detail.