-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
CoroutineScope.cancel() extension to reduce boiler-plate in a typical UI app #671
Comments
Something like val CoroutineScope.currentJob : Job
get() = TODO() Or a better name |
@fvasco Thanks. Something like
Maybe |
I agree with you, @elizarov In the You may consider to deprecate |
This is an interesting proposal, and I think it's a good idea to help minimize this boilerplate. Update: Replaced my reply after considering da16bc2. CoroutineScope.cancel() shows up in too many placesThe blocks for
|
I wonder, can inline classes allow for extending an API like this? |
@objcode I've looked at the usage via
|
After considering this for a while I think any new APIs here are not going to improve the code above For Taking a look at the code, I think the explicit How does this sound to you:
|
LGTM. I'll close this issue. |
I'm reading Android codelabs on coroutines here https://codelabs.developers.google.com/codelabs/kotlin-coroutines/index.html#4
It gave me a fresh look on the pattern we are advertising for
CoroutineScope
. There it is being cast without inheritance:This does look like bit a boilerplate, even if you have to write it once in your app. What we can do, is to have a
cancel()
extension function directly onCoroutineScope
.PRO: It is going to simplify the above code (note that
CoroutineScope(...)
constructor always creates aJob
object):CON: We have a convention of declaring "coroutine builder" functions as extensions on
CoroutineScope
. It mean, that if we havecancel()
extension onCoroutineScope
, then it will become available ascancel()
in the scope of those coroutine builder functions, which could be confusing and is completely useless (at least, I don't see a use-case forcancel()
in the scope of coroutine builder). I don't have a nice solution for this downside.Any comments?
The text was updated successfully, but these errors were encountered: