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

sitepodmatt

02/28/2019, 7:46 AM
@elizarov is there a reason Job doesn't expose parentJob : Job?. My use case would be logging/debugging aids - wanting to log the path of jobs to the root when one fails, a job trace vs a stack trace. Ive managed to hack around it with this evil code but I wondering if there a strong reason for concealing
Copy code
@UseExperimental(InternalCoroutinesApi::class)
fun Job.getParent() : Job? {
    return (AbstractCoroutine::class.java as Class<*>)?.
        getDeclaredField("parentContext")?.
        also { it.isAccessible = true }?.
        let { it.get(this) as? CoroutineContext? }?.
        let { it[Job] }
}
e

elizarov

02/28/2019, 8:45 AM
Two reasons. One is encapsulation. Parent is Job’s internal detail. If I gave you reference to my child job, then you should be able to cancel it, but not walk up to its parent (me) and cancel me. The other one is we are looking to potentially “late binding” to a parent in the future, which makers “parent” property mutable and thus harder to reason to about.
👍 1
For debugging, I’d suggest to take a look here: https://github.com/Kotlin/kotlinx.coroutines/blob/master/core/kotlinx-coroutines-debug/README.md We are considerably enhancing these debugging facilities for an upcoming release.
s

sitepodmatt

02/28/2019, 8:46 AM
Okay thanks, will take a read
4 Views