-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Weird proposal: operator invoke extension on context #428
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
Comments
Maybe (updated)
Then we have code complete narrowed down, and the meaning of UI { } is more obvious... |
@dave08 Launching new coroutines in UI is a "global" operation, since you are now responsible for making sure you'll cancel it. That is a patter we'd like to discourage in the future. See my notes in #410. We plan to make |
Not sure if |
That was a bit the idea (I just updated it a bit now), and how I would understand your usage of I proposed (along the lines of your idea) that Inside a |
@dave08 I see. That makes sense. However, I don't have a clear picture on how we could implement something like that. The idea in #410 was the we take
This does not look nicer to read than:
|
@qwwdfsad It is indeed a fertile ground for confusion. That's the challenge. However, it is Ok if people use I was actually thinking about the code that you are would be writing when you are doing UI apps and doing some processing background. Now you have to write something like this to do some background work and update UI:
Given ability to provide your own coroutine scope per #410 and combining it with this proposal, we could simplify this common pattern to:
So if you follow the pattern of always launching coroutines in background and only switching to UI context to update UI, you would not need to use Now, if you also follow a pattern of "encapsulating context", you can rewrite it like this:
This looks very clean and readable to me, especially with proper naming (like adding |
@elizarov: Hi Roman, As I checked your latest example, I noticed that you do it exactly the opposite way as I am used to do it :-). What I usualy do is I like this approach more especially when I need to do more updates on UI thread (so I don't need to switch often) and regarding some in-memory operations like List mapping etc., they're usually fast enough not to block the UI noticably for the eye, so I am fine doing it on the UI thread as well without the need to switch to another pool. So having the ability to write PS: My use cases are mainly TornadoFX apps |
@LittleLightCz You are right. I've actually played some more with various UIs and, indeed, The idea if that in your UI you should be able to define you primary UI classes to implement Having, this cleared, we'll need some nice name for various background contexts. We're plan to have
What do you think? |
This proposal is becoming much more interesting with structured concurrency and default dispatcher that can change according to the coroutine scope (e.g. default to |
@elizarov I've looked at the related issues and to be honest, I don't feel to be technically skilled enough to fully comprehend the topics discussed there 😁. Anyway just a few points on what I think I understood:
And for the rest I will give it into your hands hoping that you guys know what you're doing by making such redesign 😉. The coroutines, as they are now, are very lovely and I like them very much, so please be careful not to make them less concise or uglier Regarding the namings, generally And if you're asking me for a shorter name for PS: There is one advantage for going with lower/camel case: you don't need to hold Shift to type it! |
Another names that came to my mind: |
Another few brainstorming names: |
I think that it now makes sense as we have the launch {
touchTheUi()
Dispatchers.IO {
touchTheStorage()
}
touchTheUiAgain()
Dispatchers.Default {
heatUpTheCpu()
}
centerTextView.text = "You can now warm your hands on your device"
} |
Should I make a PR to add an I'm proposing to add it to
|
@LouisCAD 👍 Let's try it as an extension for |
I have a class that represents my dispatchers that I use to ensure my dispatchers are injected. I believe this is a good practice. class AppDispatchers(
val main: CoroutineContext = Dispatchers.Main,
val default: CoroutineContext = Dispatchers.Default,
val io: CoroutineContext = Dispatchers.IO,
) By making this an extension for |
@ZakTaccardi You can just expose |
Closing because it is fixed in 1.2.0-alpha |
@LouisCAD You can just expose CoroutineDispatcher instead of CoroutineContext No, I can't. A The following will not compile. val dispatcher: CoroutineDispatcher = Dispatchers.Default + CoroutineExceptionHandler { _, _ } |
@ZakTaccardi Then the name of your property is wrong, it's not a dispatcher, but a Adding an exception handler along with it, and treating it like its just a dispatcher is shady (may introduce unexpected bugs when exception handler is replaced) and lacks explicitness, which I mentioned in my pre-PR comment: #428 (comment) |
Ok. This is a weird, but let me shoot it here. How about instead of writing:
we could just write:
Does it make sense? 👍or 👎
The text was updated successfully, but these errors were encountered: