Hi, Is there any difference in these `scope` i.e. ...
# coroutines
k
Hi, Is there any difference in these
scope
i.e.
Copy code
private val scope = CoroutineScope(SupervisorJob() + <http://Dispatchers.IO|Dispatchers.IO>)
or
Copy code
private val scope = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO> + SupervisorJob())
?
🚫 1
Ordering doesn't matter in here?
j
No, a context is a map, with at most one value per type of context element. The order doesn't matter because the pieces of code accessing one context element will just get what they need. There could be a difference in how the elements are printed (if you were to print the context), but that would depend on the exact implementation, and functionally it will work the same.
What could be different, is if you add elements of the same type. Then the last one wins
k
perfect
Thanks for you guidance
j
Note that this last point is important if you deal with arbitrary contexts with possibly different elements at runtime. For instance:
Copy code
private val scope = CoroutineScope(someOtherContext + <http://Dispatchers.IO|Dispatchers.IO>)
vs
Copy code
private val scope = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO> + someOtherContext)
In the first case, if the externally provided context already has a dispatcher, it will be overridden, so
IO
is guaranteed in this scope. In the second case, the externally provided dispatcher (if present) will take precedence over the explicit one, so
IO
would be like a default.
k
It sounds little bit confusing, but I'll make sure what you have mention in here. Thanks