-
I am struggling with preparing my project for strict concurrency in Swift6. The problemTo make
Example:import Dependencies
struct TestClient: Sendable {
var doSomething: @Sendable () -> String
var setSomething: @Sendable (String) -> Void
}
extension TestClient: DependencyKey {
static var liveValue: TestClient {
TestClient(
doSomething: { "" },
setSomething: { _ in }
)
}
}
extension DependencyValues {
var testClient: TestClient {
get { self[TestClient.self] }
set { self[TestClient.self] = newValue }
}
}
// MARK: - Class using Dependency
final class SomeClassUsingDependency: Sendable {
@Dependency(\.testClient) var testClient // ❗️Stored property '_testClient' of 'Sendable'-conforming class 'SomeClassUsingDependency' is mutable
} The questionWhat would you suggest to be able to handle this warning properly? Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hi @niorko, this is just the reality of property wrappers and concurrency. Every property wrapper is going to have this problem because they secretly create a However, typically is very hard to make a class Or alternatively you need to synchronize the entire class using a global actor, such as |
Beta Was this translation helpful? Give feedback.
-
You can also work with Dependency struct directly, in that case you can avoid final class SomeClassUsingDependency: Sendable {
private let _testClient = Dependency(\.testClient)
private var testClient: TestClient { _testClient.wrappedValue } // just for convenience
} |
Beta Was this translation helpful? Give feedback.
Hi @niorko, this is just the reality of property wrappers and concurrency. Every property wrapper is going to have this problem because they secretly create a
var _
property, which of course does not play nicely with sendability. There was a proposal to allow for property wrappers withlet
, but it never went anywhere.However, typically is very hard to make a class
Sendable
. Does your class truly have no mutable state whatsoever? If it does, then usually you have to provide your own locking under the hood and use@unchecked Sendable
. And in that case then the warning goes away and you should make never mutate through_testClient
.Or alternatively you need to synchronize the entire class u…