-
Notifications
You must be signed in to change notification settings - Fork 111
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
Implicit Schedulers #16
Comments
See discussion history at ReactiveX/RxJava#815 prior to RxScala project. |
I think this solution is too complicated to be adopted, and some customization of schedulers is already allowed by RxJava's RxJavaSchedulersHook, so I'm closing this. |
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Originally filed by @samuelgruetter and ReactiveX/RxJava#815
This is a proposal to use Scala's implicits to improve the Schedulers part of the RxScala API, by @vjovanov and myself.
Current situtation
There are many methods depending on a Scheduler. Since we do not want to always pass the Scheduler argument explicitly, there's a second version of each method which chooses a reasonable default Scheduler, for instance
Advantages of this proposal
Compared to the current situation, this proposal brings the following adavantages:
Usage examples
There are three ways of using Schedulers:
1) With default implicits: For each method, the framework chooses a reasonable default scheduler. It can be a different Scheduler for each method: For instance, for
buffer
,ThreadPoolForComputation()
is chosen, and forfrom
,ImmediateScheduler()
is chosen.2) With explicitly specified Schedulers:
3) With custom default Schedulers:
This feature is the whole point of this proposal.
How it works
There's one marker trait (without any members) for each method in Observable which takes a Scheduler:
All method pairs of a method with Scheduler and its corresponding method without Scheduler are replaced by one single method with an implicit Scheduler parameter of type
Scheduler with DefaultXxxScheduler
, for instanceis replaced by
Further, the original
is replaced by
because we need to create new Schedulers using the
new
keyword, such that we can specifywith DefaultXxxScheduler with DefaultYyyScheduler ...
.The three ways of using Schedulers work as follows:
1) Default implicits
There's
which has to be imported by all users who don't want to customize:
This is similar to Futures requiring all users to
so users should not get too upset about it ;-)
2) With explicitly specified Schedulers
There are implicit conversions to convert a regular
Scheduler
to aScheduler with DefaultXxxScheduler
:These implicits just unwrap the underlying Java Scheduler and wrap it again in a differently labeled Scala Scheduler.
3) With custom default Schedulers
Whenever a method xxx is called without giving a Scheduler, the Scala compiler will look for an implicit value of type
DefaultXxxScheduler
, issue a "could not find implicit value" error if none was found or an "ambiguous implicit values" error if multiple implicit values matched.Advanced usage
By defining new Scheduler marker traits, users can define their own method groups which should use the same Scheduler, and new groups can also be defined in terms of smaller groups:
These could then be used as follows:
but also as like this:
Preview
An incomplete preview can be found in this branch.
The text was updated successfully, but these errors were encountered: