When using withContext in Kotlin, how to keep the new context after calling another suspend function?
For example: https://pl.kotl.in/fkrnEkYZP
fun main() = runBlocking {
val anotherBlock = suspend {
assertSame(Dispatchers.IO, coroutineContext[CoroutineDispatcher], "current CoroutineDispatcher should be still Dispatchers.IO")
// Exception in thread "main" java.lang.AssertionError: current CoroutineDispatcher should be still Dispatchers.IO. Expected <Dispatchers.IO>, actual <BlockingEventLoop@6863cde3> is not same.
}
withContext(Dispatchers.IO) {
assertSame(Dispatchers.IO, coroutineContext[CoroutineDispatcher], "current CoroutineDispatcher should be Dispatchers.IO")
anotherBlock()
}
}
Why is the coroutineContext in anotherBlock different with the parent one?