Avatar
Avatar
Iceman
@swift-5.9.2 -strict-concurrency=complete import Foundation func takeSendableClosure(_ c: @Sendable @escaping () -> ()) { Task.detached(operation: c) } let nonSendable: () -> () = {} takeSendableClosure(nonSendable) // 警告出る struct S { var nonSendableValue: () -> () @Sendable func mayUseNonSendableValue() { nonSendableValue() } } var count = 0 var s = S(nonSendableValue: { count += 1 }) takeSendableClosure(s.mayUseNonSendableValue) // 警告出ない for _ in 0..<10000 { takeSendableClosure(s.mayUseNonSendableValue) } print(count) sleep(1) print(count) (edited)
9745 9999<stdin>:7:21: warning: converting non-sendable function value to '@Sendable () -> ()' may introduce data races takeSendableClosure(nonSendable) // 警告出る (edited)