swhitty
(Simon Whitty)
1
If I have a Sendable type why aren't its methods also @Sendable func by default?
struct Foo: Sendable {
func bar() { }
}
let foo = Foo()
let fn: @Sendable () -> Void = foo.bar
// ⚠️ Converting non-sendable function value to '@Sendable () -> Void' may introduce data races
The type can automatically fulfil the requirements of a protocol and then it appears to be a @Sendable func
protocol Barable: Sendable {
@Sendable func bar()
}
extension Foo: Barable { }
let fn: @Sendable () -> Void = (foo as Barable).bar
So is it a @Sendable func or is the protocol conformance a bug?
I realise you can create a closure inferring @Sendable func from context but this is not what I am asking about.
let fn: @Sendable () -> Void = { foo.bar() }
2 Likes
swhitty
(Simon Whitty)
2
This pitch proposes that @Sendable func should be inferred on types that conform to Sendable.
But methods of a nominal type cannot capture anything but the object instance itself. Thus, the proposed simplifications are:
- the inference of
@Sendable on all methods of a type that conforms to Sendable.
- the inference of
@Sendable on all non-local functions.
- the prohibition of marking a method
@Sendable if the object type does not conform to Sendable.