What does Sendable mean for a Swift type you write?
Predict first
Does this compile?
final class Counter: Sendable {
var count = 0
}
- Yes
- No
Mental model. Sendable is a passport stamp: it says this value can cross a border without anyone getting hurt.
Sendable means the value can cross concurrency domains without a data race. Safe cases: immutable structs/enums of Sendable fields, actors (by identity, since their state is isolated), and final classes with immutable let storage.
A mutable class can opt in with @unchecked Sendable — but then you own the synchronization proof. Swift 6 language mode turns missing Sendable on a boundary crossing into an error.
A Sendable class must be final and have only immutable, sendable stored properties. Mutable state needs an actor, a lock with @unchecked Sendable, or a Mutex.
Common traps
- “Actors are Sendable.” Yes — but their properties are not automatically safe to hand out. Returning a mutable reference from an actor leaks isolation.
- “
@unchecked Sendablemakes it safe.” It only silences the compiler. If you did not add a lock or prove immutability, you now have an undiagnosed race. - Marking a struct
Sendablewhen it contains a class. The class must also be Sendable, or the conformance is rejected.
They might follow up with
- What is a
sendingparameter? It transfers ownership of a non-Sendable value across an isolation boundary, so the caller cannot use it afterwards. - What is region-based isolation? Swift can prove a value is not referenced elsewhere and allow it to cross even without a
Sendableconformance.
Related questions
- 1.10.3 Macros vs property wrappers vs result builders — one sentence each.
- 1.9.1 Why is String not a RandomAccessCollection of Character? Why is string[i + 1] not O(1)?
← How does Codable synthesis work? When do you write CodingKeys or a… Macros vs property wrappers vs result builders — one sentence each. →