Some thoughts on the proliferation of Sendable

let’s go through these exceptions one by one.

UnsafePointer

UnsafePointer is a textbook example of an unsafe construct that gets wrapped by some “safe” abstraction which is ostensibly Sendable, but only because the lax concurrency mode doesn’t require the wrapper type to only contain Sendable stored properties.

under the strict concurrency mode, it raises warnings because the wrapped pointer is not Sendable (SE-0331).

but such a type would usually still carry a

@available(*, unavailable, message: "not Sendable because ...")
extension PointerWrapper:Sendable

to document why it is not Sendable, and to force the warning to appear even with the lax concurrency mode.

this isn’t a very common case, it’s just an example of a place where being able to write PointerWrapper:~Sendable would be helpful.

Any

Any is already a problem when using the strict concurrency mode, it inevitably needs to be refactored into any Sendable in code that uses concurrency. Any without any Sendable qualifications just isn’t that useful in a concurrent context.

this is a weird inverse of the problem we have with ~Copyable and Any, and you could imagine an alternate reality where Any didn’t imply Copyable and we had a similar problem with underconstrained existentials that cannot be copied.

Classes

i personally think classes with mutable state are going to become an endangered species as the strict concurrency mode becomes more popular, they just don’t compose very well with concurrency.

for a concrete case study, you can browse through some of the discussions about SwiftNIO’s ClientBootstrap class, which was conceived before the language had first-class concurrency support, and is now widely viewed as ill-adapted for modern concurrency features.

i personally think a lot (not all, but a lot) of these types will have to evolve into actors if they need shared state, and structs/enums otherwise.

conclusion

i don’t think these exceptions really contradict my argument that Sendable is too fundamental a conformance to have to be sprayed everywhere preemptively, or reported as an issue when it is missing. the types that are ~Sendable are the special cases, and i think as the warnings pile up they are going to become less common and the ones that remain will become more-clearly documented as such.

3 Likes