-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Deprecate abstract coroutine context keys #4522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -122,19 +122,17 @@ internal suspend inline fun <R> selectUnbiasedOld(crossinline builder: SelectBui | |
| scope.initSelectResult() | ||
| } | ||
|
|
||
| @OptIn(ExperimentalStdlibApi::class) | ||
| private fun <T> CancellableContinuation<T>.resumeUndispatched(result: T) { | ||
| val dispatcher = context[CoroutineDispatcher] | ||
| val dispatcher = context[ContinuationInterceptor] as? CoroutineDispatcher | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the explicit I'd mildly prefer all usages in the codebase to be either all I don't have a strong opinion, just noticing a tiny inconsistency. Feel free to discard if it makes some sense to keep them different.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The specific syntax doesn't matter here and can be chosen abritrarily, but for the deprecation message, there is an important distinction. A We could say "use the |
||
| if (dispatcher != null) { | ||
| dispatcher.resumeUndispatched(result) | ||
| } else { | ||
| resume(result) | ||
| } | ||
| } | ||
|
|
||
| @OptIn(ExperimentalStdlibApi::class) | ||
| private fun CancellableContinuation<*>.resumeUndispatchedWithException(exception: Throwable) { | ||
| val dispatcher = context[CoroutineDispatcher] | ||
| val dispatcher = context[ContinuationInterceptor] as? CoroutineDispatcher | ||
| if (dispatcher != null) { | ||
| dispatcher.resumeUndispatchedWithException(exception) | ||
| } else { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a bit of a nuisance.
I would consider coupling this change with introduction of
.dispatcherand.dispatcherOrNullextensions onCoroutineContext(not sure about theCoroutineScope).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you mean, but this pattern is very niche anyway: https://grep.app/search?f.lang=Kotlin®exp=true&q=%5B%5E+%5D%5C%5BCoroutineDispatcher%5C%5D
In addition, in most cases, using
ContinuationInterceptorinstead ofCoroutineDispatcherdoesn't make any functional difference (or even makes the code a bit more flexible), which means directing the few existing cases to.dispatcheris not ideal.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've figured, but still see it as a good idea:
capitalize) for users who do use thatThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My 2 cents:
The fact that something is niche shouldn't be the reason to dismiss/remove it.
It should only be a secondary factor, if a main reason justifies the removal/dismissal of something, IMO.
Also, beware that
grep.appisn't finding all usages that GitHub search would.For this case, your link leads to 66 usages, while I got 338 with public-only GitHub search: https://github.com/search?q=%22t%5BCoroutineDispatcher%5D%22+language%3AKotlin&type=code
If the measuring tool you use is minimizing usage of stuff, you'll end believing things are much more niche than they actually are.
That would risk churning more Kotlin devs than you would think if actions are made based on flawed data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Louis,
Since you're raising the topic of grep.app's reliability for the second time, I assume you'd appreciate an extended description of the methodology.
Yes, you are right, if something is barely used, it's not a reason to remove it or make it intentionally inconvenient. The criterion for inclusion of an API is that it should either solve a common need or not be easily expressible, but for removal, the harm of the API must outweigh its popularity. A terribly harmful API may get modified or removed even at the cost of far-reaching changes, whereas to remove an API that's just non-idiomatic and has a better alternative, it's enough for it to be niche.
grep.app is indeed not finding all usages. Neither does github, as there is a lot of code that's not on github at all. This is not a problem for our analysis, as the goal is not to evaluate how many codebases will be affected, but what fraction of them correspond to which measure of popularity. https://grep.app/search?q=kotlinx.coroutines.Job or https://grep.app/search?q=kotlinx.coroutines.flow.combine are examples of widely used APIs. https://grep.app/search?q=kotlinx.coroutines.channels.toList is niche.
The distribution of hits on grep.app is a bit different from the one on GitHub, as grep.app filters out duplicate files. If a user copy-pastes a library into their project, it results in additional hits on GitHub but not on grep.app. This means that not only absolute numbers will be different between GitHub and grep.app, but also the relative ones. I link to grep.app, as I believe that copies of libraries are not important for our analysis, since users are unlikely to maintain them on their own, so grep.app's results are more representative.
Another reason to use grep.app is to understand the usage patterns. This is often much more significant than it is to just count usages, as that way, we can learn more about why people are using an API at all and whether we can provide them with a better alternative. Removal of duplicates on the grep.app's side helps us more conveniently sift through the files.
To sum it up, yes, we are aware of the tool's limitations, which is why we would never say "this change will affect 100 codebases." We have no way of knowing. What we can fairly confidently say is, "the usage is dominated by uncommon requirements of a few large foundational codebases that have the resources to fix this."
Please let me know if this answers your doubts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello Dmitry,
Thanks for the answer!
I appreciate it, and it makes sense to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I may chip in my 2 cents, I believe we should prioritise moving faster on the minor occasions, rather than being excessively thorough. So I vote not to do anything and merge this already :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided against adding the
.dispatcherextension, as the typical use case of duplicating another coroutine's dispatcher doesn't need to distinguish betweenContinuationInterceptorandCoroutineDispatcher, which means the extension would be.continuationInterceptor.Otherwise,
.dispatcher[OrNull]would hide a subtle bug, as the code would read as, "This context has no dispatcher", when in fact, there is a continuation interceptor, even if it's not actually a dispatcher..continuationInterceptordoesn't seem any better than[ContinuationInterceptor]to me, so I don't see the point.