It seems like CoroutineContext
s could do something similar, since they provide scoped, immutable but overridable data to all children. Something like:
data class MyData(val data: Foo) : CoroutineContext.Element {
public companion object Key : CoroutineContext.Key<MyData>
}
withContext(MyData(data = foo)) {
// access the data from any child context
val foo = currentCoroutine[MyData]?.data
}
Roman Elizarov from JetBrains has said before that they won’t be using virtual threads in coroutine dispatchers because their dispatchers are highly tuned to avoid context switching and virtual threads don’t have an API that would allow them to replicate that.