SE-0311 (2nd review): Task-local values

I was just wishing for a “scoped context” in some synchronous code, and a random thought occurred to me: would it be possible to have a runtime function that creates a task, but takes a synchronous closure and executes it without introducing any suspension points?

It seems to me that this would allow use of task-local values for side-channel communications within existing synchronous patterns. It would still be “structured” by virtue of being semantically attached to a stack frame rather than a thread, and would propagate through async-the-function but not know about threads or dispatch queues.

As for the API, we (once again) run into the limitation that “requires a task context” and “can have suspension points” are conflated in a single effect. I see two “easy” workarounds:

  • An unsafeWithValue that isn’t marked async but crashes if there is no current task
  • A withSynchronousValue that creates the proposed “synchronous task” and binds a task-local value in one shot.

Neither of these is entirely satisfactory, but both seem better to me than completely excluding blue functions from the implicit-context-passing fun.

1 Like