SE-0311: Task-local values

I don't speak for the core team, only for myself, but I think it would be fine to have an adhoc check for this. We aspire to have as much as possible in the library, but "perfection" shouldn't get in the way of "good".

I think it is really really important to figure out the global variable story here. This approach with property wrappers is directly gunning for the recommendation that I was making. However, if we go that far, we should just make it a language thing, and allow traceID = 1234 and print(traceID) with language support.

The other direction to go is to more directly embrace the fact that this is an indirect lookup and is inherently scoped-dictionary-like. You could enable something like Task.locals[&traceID] (safe because traceID could be guaranteed to have be a global per the check above) or potentially use keypaths.

Another related design question is: is the withValue scoping mechanism is actually important to achieving your goals? As far as I can tell, it provides a very simple approach to make the path compression stuff work nicely: if you have child tasks, they can be cranking along and the parent can go ahead and make more scoped things happen without disturbing them.

However, there are alternative implementation approaches that would make this work (e.g. use the refcount machinery like the COW implementation does). This would allow you to decouple the scoping from the value setting, allowing you to provide a properly unstructured get/set sort of interface (which would be very familiar and dictionary like) but then also provide an explicit "SaveAndRestore" style scoping operation for those that want to make a change that only effects the code below in a lexical region.

-Chris

6 Likes