Coder Social home page Coder Social logo

Comments (14)

voddan avatar voddan commented on September 23, 2024 6

My issue with coroutine dispatchers not being organized in a hierarchy is poor discoverability. For example, if I want to use a coroutine builder, but forgot the spelling of a dispatcher I need, there is currently no better option than reading the documentation.

I propose to introduce a global singleton Dispatcher for serving as a hierarchy root. Then all coroutine dispatchers can be its extension properties val Dispatcher.Unconfined get() = ... and be used as Dispatcher.Unconfined.

The cost is that we have to use the prefix. The benefit is that IDE now lists all dispatchers for me.

from kotlinx.coroutines.

chrisbanes avatar chrisbanes commented on September 23, 2024 4

I like the flexibility of @LouisCAD's suggestion. Being able to provide both Android.Main (async) and Android.UI (vsync) dispatchers, but I agree with @adamp in #427 (comment) that having two dispatchers would be confusing too.

I still think Dispatchers.Main is an unfortunate name since it's such a generic noun. Maybe Dispatchers.Android to be similar to JavaFx and Swing?

from kotlinx.coroutines.

elizarov avatar elizarov commented on September 23, 2024 2

There is also a related issue of naming various functions that take dispatchers as their arguments. Might renaming run to runOn help? See also discussion here: https://discuss.kotlinlang.org/t/calling-suspending-function-from-regular-function/3670

from kotlinx.coroutines.

elizarov avatar elizarov commented on September 23, 2024 2

I've reopened this discussion in light of structured concurrency (#410) and the proposed change for the Android dispatcher (#533). The goal is to address two concerns:

  • Discoverability of different dispatchers.
  • Readability of the code, given the fact that dispatcher is always passed as context parameter and there are different types of context element, so it may not be visually obvious which one is a dispatcher.

The concrete proposal I have on the table is to group all the dispatcher references under Dispatchers object:

  • Dispatchers.Default — a default dispatcher for background asynchronous tasks (currently backed by CommonPool, a new dispatcher in the future).
  • Dispatchers.IO — a dispatcher for blocking background operations (#79).
  • Dispatchers.Main — a dispatcher for Android Main Thread (#533).
  • Dispatchers.Swing — a dispatcher for Swing Event Dispatch Thread.
  • Dispatchers.JavaFx — a dispatcher for JavaFx Application Thread.

from kotlinx.coroutines.

chrisbanes avatar chrisbanes commented on September 23, 2024 2

I think Dispatchers.Main is going to confuse people. Maybe Dispatchers.Android.Main to make it clearer?

from kotlinx.coroutines.

LouisCAD avatar LouisCAD commented on September 23, 2024 2

While Dispatchers.Android.Main is a bit more verbose, it is more consistent against Dispatchers.JavaFx and similar, and allows for Dispatchers.Android.UI extension to access the default UI thread with vsync if needed (see #507).

As LifecycleOwners (Activity, Service, etc) are probably going to be CoroutineScopes by themselves, they would default to Dispatchers.Android.Main, so you'd most likely never have to write it by yourself in your codebase.

from kotlinx.coroutines.

elizarov avatar elizarov commented on September 23, 2024 2

@chrisbanes Let me clarify, that Dispatchers.Main is going to be available only when you program for Android and add kotlinx-coroutines-android dependency, so there should not be any confusion about which Main is that. On the other hand, both JavaFx and Swing can be available when you program for Java Desktop, for example, that is why they are named in a more explicit way.

from kotlinx.coroutines.

elizarov avatar elizarov commented on September 23, 2024 1

I'm not convinced that a potential name clash is actually a problem. Can you give some examples as to where you have encountered it?

The argument against this change is that dispatchers are exclusively used as arguments to various coroutine builders: launch, async, produce, etc. This particular usage context gives enough disambiguation to make any kind of "Dispatcher" suffix superfluos (or so it seems).

from kotlinx.coroutines.

fvasco avatar fvasco commented on September 23, 2024 1

Hi @elizarov,
I have a problem naming a Vert.x's dispatcher, following current convention its name should be "Vertx"?

Any suggestion in well accepted, thanks.

from kotlinx.coroutines.

elizarov avatar elizarov commented on September 23, 2024 1

I'd give it a short name, like Vx. However, I'm still keeping this naming issue open to review the naming before we finalise 1.0 version of kotlinx.coroutines. Short names might be good for slide-ware, but it is not that clear-cut for a larger project.

from kotlinx.coroutines.

fvasco avatar fvasco commented on September 23, 2024

This issue raised on my first approach on coroutines.

If we consider Java's List then we can have ArrayList, LinkedList, EmptyList and so on. It is simple to aggregate all these on a single cap.

"Unbounded" and "Swing" are too heterogeneous names, and the argument for "async" method is "CoroutineContext" not "CoroutineDispatcher", so I can wrongly use "Swing" as a coroutine's L&F.

In simple case the code a trained programmer can understand correctly the statement: produce(Unconfined) ...
I fear that in more complex use case a wrong name can lead to confuse code.

from kotlinx.coroutines.

elizarov avatar elizarov commented on September 23, 2024

@voddan That is an interesting idea. I like it more than just giving them longer names. like XXXDispatcher.

from kotlinx.coroutines.

ScottPierce avatar ScottPierce commented on September 23, 2024

I agree that the coroutine dispatcher naming conflict isn't really an issue. It's somewhat nice that it's short and sweet for something that will be commonly used: launch(UI) { ... }

from kotlinx.coroutines.

elizarov avatar elizarov commented on September 23, 2024

Closing this issue. We'll leave the general naming approach as it is (a few top-level names).

from kotlinx.coroutines.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.