any one here can you explain to me what this code ...
# coroutines
t
any one here can you explain to me what this code means?
val coroutineContext: CoroutineContext get() = Job() + Dispatchers.Main
w
It is a custom implementation of the operator `plus`:
Copy code
/**
     * Returns a context containing elements from this context and elements from  other [context].
     * The elements from this context with the same key as in the other one are dropped.
     */
    public operator fun plus(context: CoroutineContext): CoroutineContext =
        if (context === EmptyCoroutineContext) this else // fast path -- avoid lambda creation
            context.fold(this) { acc, element ->
                val removed = acc.minusKey(element.key)
                if (removed === EmptyCoroutineContext) element else {
                    // make sure interceptor is always last in the context (and thus is fast to get when present)
                    val interceptor = removed[ContinuationInterceptor]
                    if (interceptor == null) CombinedContext(removed, element) else {
                        val left = removed.minusKey(ContinuationInterceptor)
                        if (left === EmptyCoroutineContext) CombinedContext(element, interceptor) else
                            CombinedContext(CombinedContext(left, element), interceptor)
                    }
                }
            }
Sorry don't know much to explain WHAT that is doing per say, if that is what you are looking for 😬
z
Whatever it's trying to do, it's probably doing it wrong. That code will create a new, parentless
Job
every time the property is read, which breaks structured concurrency in multiple ways and makes it very easy to write code that looks idiomatic and correct but is full of bugs. That context should probably be created once and then held onto, and the job should probably have a parent or at least be explicitly cancelled at some point.
2
☝🏼 1
☝️ 3