thana
08/04/2019, 11:22 AMcoroutineContext
documentation "By convention, should contain an instance of a job to enforce structured concurrency.". What does it mean? It'spossible that a context does NOT contain a job? what are the consequences if a job instance is missing?Dominaezzz
08/04/2019, 11:31 AMsuspend fun main(args: Array<String>)
.kotlinx.coroutines
for that and that's where Job
comes from.Job
is missing, cancellation can't happen. (I'm not sure if I should say "easily" or "not at all" here).Marko Mitic
08/04/2019, 12:00 PMDominaezzz
08/04/2019, 12:02 PMgildor
08/04/2019, 1:49 PMsuspend main
, because it doesn't have scope so not related to structured concurrency and scopes, this convention only about coroutine scopes implementation, because if it doesn't have Job, it doesn't have child/parent relationship and cannot be cancelledDico
08/04/2019, 4:34 PMthana
08/05/2019, 5:40 AMgildor
08/05/2019, 5:42 AMthana
08/05/2019, 5:42 AMgildor
08/05/2019, 5:42 AMthana
08/05/2019, 5:44 AMThread
from a structuring point of view?gildor
08/05/2019, 5:44 AMthana
08/05/2019, 5:44 AMThread
is basically fire and forgetgildor
08/05/2019, 5:45 AMthana
08/05/2019, 5:53 AMgildor
08/05/2019, 5:54 AMthana
08/05/2019, 5:55 AMgildor
08/05/2019, 5:55 AMthana
08/05/2019, 5:55 AMgildor
08/05/2019, 5:55 AMthana
08/05/2019, 5:56 AMgildor
08/05/2019, 5:56 AMthana
08/05/2019, 5:57 AMgildor
08/05/2019, 5:58 AMywah but that’s what you have to do in javanot sure what you mean. See, coroutines by itself and their basic primitives in stdlib has nothing to do with threading, it’s language feature to simplify writing asynchronous code in a sequential style, that’s all, it provides you primitives to convert callback to suspend fucntion Threading, cancellation, scopes, async, launch etc are features of kotlinx.coroutines library which provides high level primitves based on Kotlin Coroutines
how would we use coroutines on a world without kotlinx.coroutines? what would be there use?They are pretty low level, and this is whole point of them. For example #arrow (and some other libraries) use coroutines but do not use kotlinx.coroutines Again, it’s just primitives used by other libraries
thana
08/05/2019, 5:59 AMgildor
08/05/2019, 6:04 AMhow would we use coroutines on a world without kotlinx.coroutines? what would be there use?Just a very simple example, which has nothing to do with threading or cancellation. Imagine you have this asynchronous API:
fun <T> sendRequest(onSuccess: (T) -> Unit, onError: (Throwable) -> Unit) {
TODO()
}
suspend fun <T> awaitRequest(): T {
return suspendCoroutine { cont ->
sendRequest<T>(
onSuccess = { cont.resume(it) },
onError = { cont.resumeWithException(it) }
)
}
}
suspend fun main() {
val result = awaitRequest<T>()
println(result)
}
See, no callbacks, no, nesting, only stdlib APIsthana
08/05/2019, 6:09 AMSince the initial rollout of Kotlin coroutines as an experimental feature in Kotlin 1.1 in the beginning of 2017 we’ve been working hard to explain the concept of coroutines to the programmers who used to think of concurrency in terms of threads, so our key analogy and motto was “coroutines are light-weight threads”.
gildor
08/05/2019, 6:11 AMthana
08/05/2019, 6:12 AMgildor
08/05/2019, 6:13 AMthana
08/05/2019, 6:14 AMgildor
08/05/2019, 6:14 AMthana
08/05/2019, 6:14 AMCoroutineScope
or coroutineContext
it's written that any context should contain a job to enforce structured concurrencygildor
08/05/2019, 6:17 AMNote that an implementation of CoroutineScope also defines an appropriate coroutine context for your UI updates. You can find a full-blown example of CoroutineScope implementation on its documentation page.
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html
it’s written that any context should contain a job to enforce structured concurrencySo, I don’t see contradiction here
thana
08/05/2019, 6:21 AMgildor
08/05/2019, 6:24 AMGlobalScope.launch
), and this scope can be cancelledthana
08/05/2019, 6:25 AMJob
, right?gildor
08/05/2019, 6:25 AMcoroutineContext
propertyCoroutineScope.cancel()
just check what it doesthana
08/05/2019, 6:28 AMerror("Scope cannot be cancelled because it does not have a job: $this")
ha