Avatar
treastrain / Tanaka.R 10/19/2025 6:45 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 ) throws -> R { var currentOperation = operation for pair in repeat each valuesDuringOperation { let operationToWrap = currentOperation currentOperation = { try pair.0.withValue(pair.1, operation: operationToWrap, file: file, line: line) } } return try currentOperation() } try withTaskLocalValues( (GlobalValues.$property1, 2), (GlobalValues.$property2, "World"), (GlobalValues.$property3, 4), ) { print(GlobalValues.property1, GlobalValues.property2, "\(GlobalValues.property3, default: "nil")") }