Make context combination more explicit with `copy()`
See original GitHub issueProposed changes:
- Add
CoroutineContext.copy(....)
- Remove the optional
parent
parameter from coroutine builders - (Possibly) deprecate
CoroutineContext#plus(CoroutineContext)
to use only in library code
Pros
- very explicit about what it does (e.i. copies the context)
- explicit about the non-commutativity of the operation (`context1 + context2 != context2 + context1)
- explicit about what parts of the context it changes (e.i.
parent
orname
)
Cons
- harder to interact between libraries
Possible implementation
fun CoroutineContext.copy(
parent: Job? = null,
name: String? = null,
id: Long? = null,
from: CoroutineContext? = null
) {
var res = this // not memory efficient!
if(from != null) res = res + from // should go first
if(parent != null) res = res + parent
if(name != null) res = res + CoroutineName(name)
if(id != null) res = res + CoroutineId(id)
return res
}
Usage example:
Before:
launch(context + job) {
...
}
launch(context, parent = job) {
...
}
launch(parent = job) {
...
}
launch(context + anotherContext) {
...
}
launch(context + job + anotherContext) {
...
}
After:
launch(context.copy(parent = job)) {
...
}
launch(context.copy(from = anotherContext)) {
...
}
launch(context.copy(parent = job, from = anotherContext)) {
...
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:19 (9 by maintainers)
Top Results From Across the Web
C++ compiler 'shallow' copies and assignments - Stack Overflow
Explicitly "shallow copy" a pointer; Mix the above two based on what you actually want! Initialize member variables with default/custom values ...
Read more >Deep, Shallow and Lazy Copy with Java Examples
There are several ways to copy an object, most commonly by a copy constructor or cloning. We can define Cloning as “create a...
Read more >Loading Related Entities - EF6 - Microsoft Learn
Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading. The techniques shown in this ...
Read more >Explicit type conversion - cppreference.com
Converts between types using a combination of explicit and implicit conversions.
Read more >Spring Web Contexts - Baeldung
In this article, we're going to analyze and explain the most common options that Spring offers. 2. The Root Web Application Context.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Proposed change makes context combinations less readable, does not add any value, and
copy
name is kind of misleading because it does not copy anything. This discussion could have a place in the corresponding KEEP, but now it is too late anyway.And commutativity is not a property of
plus
operator in general.If I have to reuse context, I also put it in a property. But I build the context as usual with the
+
operator.That being said, it is pretty rare. Most of the time it is easier, and more expressive to do it when the coroutine is started (that way the reader doesn’t have to guess what’s in the context).
I don’t think readability is incompatible with brevity. I know it is subjective, but I find the following fairly easy to read: