-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert CoroutineContext
into Executor
#1450
Comments
CoroutineDispatcher
into Executor
CoroutineContext
into Executor
There is no "nice" public API like that, but you can already write without resorting to any kind of experimental API:
The only missing piece in this story is to provide a nicer API either via |
Couldn't you implement an Or a simpler implementation that doesn't require knowing the dispatcher contracts but has a bit more overhead could just be: CoroutineContext.asExecutor() = object : Executor {
private val scope = CoroutineScope(this@asExecutor)
override fun execute(command: Runnable) {
scope.launch { command.run() }
}
} |
Ok. It's easier to implement than to discuss. See PR #1457 |
Is it doable to use a |
Any given |
@zach-klippenstein For my use case, it would be fine to throw if the AppDispatchers(
val main: CoroutineContext = Dispatchers.Main,
val default: CoroutineContext = Dispatchers.Default,
val io: CoroutineContext = Dispatchers.IO,
)
|
@ZakTaccardi You can write the following simple extension for your use-case: fun CoroutineContext.asExecutor(): Exector =
(get(ContinuationInterceptor) as CoroutineDispatcher).asExecutor() |
I have to use an API that accepts an
Executor
as a parameter. How can I create anExecutor
that is backed byDispatchers.Default
?Can a
CoroutineDispatcher.asExecutor(): Executor
function be added to support this use case?Even better would be a
fun CoroutineContext.asExecutor(): Executor
function. I would imagine if the receiverCoroutineContext
does not have aContinuationInterceptor
, this function would throw.The text was updated successfully, but these errors were encountered: