https://kotlinlang.org logo
#coroutines
Title
# coroutines
n

nwh

07/20/2018, 7:39 PM
I'm almost certain I know the answer to this, but just in case: Is it possible to specify the type of CoroutineScope for a given CoroutineDispatcher or a CoroutineContext? For example, a context/dispatcher for a database that will pass the connection to the scope?
e

elizarov

07/20/2018, 9:18 PM
Sorry, but I don’t understand your question. What are you trying to achieve?
n

nwh

07/21/2018, 12:10 AM
Something like..
Copy code
launch(DB) { /* here the context of the database is automatically available */ }
g

gildor

07/21/2018, 2:03 PM
You can lookup context inside of coroutine scope, context is available inside of coroutine
g

groostav

07/22/2018, 10:22 PM
yeah, so statically no, but at runtime yes. You could then provide your own:
Copy code
//damn these narrow thread columns...
fun launchInDBContext(
    moreContext: CoroutineContext, 
    block: suspend DBScope.() -> Unit): Job{
  return launch(DBContext + moreContext){
    val directScope = this;
    val dbContext = directScope[DBContext.Key]
    val modifiedDBScope = DBScope(directScope, dbContext)
    newScope.block()
  }
}
you can probably even make that
inline
with
crossinline
on the lambda.
2 Views