Jeff Lockhart
03/18/2024, 7:00 PMcoroutineContext[CoroutineDispatcher] without requiring @OptIn(ExperimentalStdlibApi::class)? CC: @BlakeSam
03/19/2024, 7:50 AMcoroutineContext[ContinuationInterceptor] as? CoroutineDispatcherJeff Lockhart
03/19/2024, 4:36 PMAbstractCoroutineContextKey, which CoroutineDispatcher implements, that's marked ExperimentalStdlibApi. But CoroutineContext.Key is not a ExperimentalStdlibApi, even though AbstractCoroutineContextKey is the base class for CoroutineContext.Key. 🤔
What's the difference between these APIs? And why is one experimental, and the other not?Sam
03/19/2024, 4:41 PMContinuationInterceptor, and that's just a regular coroutine context element with a regular key. Nothing experimental or unexpected.
A CoroutineDispatcher is a subtype of ContinuationInterceptor. So when we write coroutineContext[CoroutineDispatcher], we're looking not just for a ContinuationInterceptor, but for a specific subtype of interceptor. That means CoroutineDispatcher.Key is a special type of key, sort of like a "sub-key" of ContinuationInterceptor.Key. I guess the exact specifics of what this means and how it should work are the bit that's experimental.Jeff Lockhart
03/19/2024, 4:57 PMCoroutineContext with a CoroutineDispatcher, but then can't decompose it using the same type. Rather, you have to identify the supertype that implements CoroutineContext.Key directly and cast it, obfuscating the code's intent as well.