question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make context combination more explicit with `copy()`

See original GitHub issue

Proposed 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 or name)

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:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
qwwdfsadcommented, Nov 6, 2018

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.

0reactions
jcornazcommented, Jun 8, 2018

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:

val wellNamedContext = dispatcher + exceptionHandler + job
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found