`Sendable` values in non-`Sendable` containers

It works (I think) if you turn your var someAsyncValue into a function and pass the caller's isolation along:

class Dependency {
    let source = Source()

    func someAsyncValue(isolation: isolated (any Actor)? = #isolation) async -> Int {
        await source.someAsyncValue
    }
}

struct Container {
    let dependency = Dependency()

    @MainActor func worksFineToo() async {
        _ = await self.dependency.someAsyncValue() // ✅
    }
}

This compiles in Swift 6 mode and seems to work fine in my very limited testing.

So you have to add a pair of parentheses at the call site and the implementation of the convenience function requires more boilerplate. You have to decide whether this is good enough for you.

Passing a dynamic isolation to computed properties is mentioned as a future direction in SE-0420. And if [Pitch] Inherit isolation by default for async functions gets implemented, your code should work as-is, I think.

1 Like