Avatar
treastrain / Tanaka.R 10/19/2025 7:12 PM
@swift-6.2.1 import Foundation enum GlobalValues { @TaskLocal static var property1 = 1 @TaskLocal static var property2: String = "Hello" @TaskLocal static var property3: Int? = nil } @discardableResult @inlinable func withTaskLocalValues<R, each V: Sendable>( _ valuesDuringOperation: repeat (TaskLocal<each V>, each V), operation: () throws -> R, file: String = #fileID, line: UInt = #line ) rethrows -> R { try withoutActuallyEscaping(operation) { var operation = $0 for pair in repeat each valuesDuringOperation { operation = { [operation] in try pair.0.withValue(pair.1, operation: operation, file: file, line: line) } } return try operation() } } withTaskLocalValues( (GlobalValues.$property1, 2), (GlobalValues.$property2, "World"), (GlobalValues.$property3, 4), ) { print(GlobalValues.property1, GlobalValues.property2, "\(GlobalValues.property3, default: "nil")") } (edited)