@Sendable does not seem to apply to reference types

It seems in the latest version of 5.5, marking a function as Sendable only applies to value types and reference types declared as var. This compiles:

class Person {
    var name = ""
}

func doStuff(_ block: (@Sendable () async throws -> Void)) async throws {
    try await block()
}

func main() async throws {
    let person = Person()
    try await doStuff {
        person.name = "Jon"
    }
}

But I would assume it should not? Is this the intended behaviour or has this not been implemented yet?

Hey there, this writeup should provide you most of the answers: Concurrency in Swift 5 and 6

Short version: we're on the road to Swift 6 which will enable all those checks and make them hard errors. In Swift 5.5 not everything is a hard error yet, but you can opt into more aggressive checking with optional compiler flags (e.g. -warn-concurrency enables Sendable checking).

Hope this helps!

1 Like