Skip to content
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

Allow queries to be eagerly constructed #488

Merged
merged 7 commits into from
Jun 22, 2024

Conversation

kyri-petrou
Copy link
Collaborator

An issue with ZQuery that makes it hard to optimize in libraries like Caliban is that it doesn't provide any means to eagerly construct queries who's values can be extracted without executing the queries via the ZIO runtime.

Let's take this as an example:

val allCountryNames = Set(???)
val datasource = ??? // DB

def amIAllowedToDrive(countryName: String, age: Int): ZQuery[Any, Throwable, Boolean] = {
  if (age < 16) ZQuery.succeed(false)
  else if (!allCountryNames.contains(countryName)) ZQuery.fail(new Throwable("invalid country name"))
  else datasource.query(???)
}

In the example above, the first 2 branches do not require us to run the ZQuery in order to evaluate it as the outcome is already known.

This PR exposes xNow variants of different methods that allow users to eagerly construct ZQueries. Libraries (like Caliban) can then check if a ZQuery is eagerly constructed via the asExitMaybe and asExitOrElse methods and extract the value/failure from it without the need of running the ZQuery and providing a Cache to it.

ghostdogpr
ghostdogpr previously approved these changes Jun 22, 2024
/**
* Eagerly constructs a query that succeeds with the specified value.
*/
def succeedNow[A](value: A): ZQuery[Any, Nothing, A] =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we ok with having this part of the public API? I know historically the core has made *Now stuff package private to avoid footguns.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I think it's okay. Most (if not all) other frameworks, including Scala's Future (which I know isn't the best example because it's not referentially transparent) expose methods for eagerly constructing monads. I think in ZIO having the *Now methods makes less sense because it has Exit. In ZQuery users don't really have that option.

paulpdaniels
paulpdaniels previously approved these changes Jun 22, 2024
@kyri-petrou kyri-petrou dismissed stale reviews from paulpdaniels and ghostdogpr via b486cfc June 22, 2024 22:41
@kyri-petrou kyri-petrou merged commit 96f9a1f into zio:series/2.x Jun 22, 2024
27 checks passed
@kyri-petrou kyri-petrou deleted the eager-optimizations branch June 22, 2024 22:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants