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

Robert Menke

10/16/2018, 3:32 PM
Hey guys, I’m trying to understand `Job`’s
cancel
method. I’m working on creating a class that implements
CoroutineScope
and I’m following the example here https://github.com/Kotlin/kotlinx.coroutines/blob/master/core/kotlinx-coroutines-core/test/guide/example-context-10.kt. I would expect that
job.cancel()
would return true in this case since the invocation of cancel is what cancels the job, however, when I print the result of
job.cancel()
I get false.
Copy code
fun destroy() {
     val temp = job.cancel()
     print(temp.toString()) //prints "false" even though I would expect it to print "true"
}
Am I misunderstanding cancel?
e

elizarov

10/16/2018, 3:39 PM
Documentation on cancel result is wrong. In fact
cancel
on
Job()
always returns
false
at them moment. It might change.
r

Robert Menke

10/16/2018, 3:41 PM
@elizarov Thanks for the clarification! Any thoughts on writing tests for coroutine cancelation scenarios?
e

elizarov

10/16/2018, 4:09 PM
We have tons of test, but they are all useless, since we don’t have any real use-cases for that boolean. It is just a left-over from earlier work. The proposal is to remove it: https://github.com/Kotlin/kotlinx.coroutines/issues/713
👍 1
d

Derek Seroky

10/16/2018, 4:19 PM
Thanks @elizarov for the information! The only (current) use we had for the boolean was in unit testing the coroutine to ensure that it was cancelled properly, but we'll figure out another way.
👍 1
3 Views