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
Implement serviceWith #4937
Implement serviceWith #4937
Conversation
b1e1850
to
be1ad2c
Compare
* def foo(int: Int) = ZIO.serviceWith[Foo](_.foo(int)) | ||
* }}} | ||
*/ | ||
def serviceWith[Service]: ServiceWithPartiallyApplied[Service] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for the naive question, but why is the "partially applied" pattern used in this scenario? I was under the impression that it helps with type inference if the method is supposed to walk, talk and quack as though it had multiple parameter lists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, Adam! 😄 Ask all the questions you have!
Unfortunately, one cannot partially apply type parameters. We need to use this pattern if we want to be able to call this method as such:
ZIO.serviceWith[Foo](_.foo(int))
If it didn't use this awkward PartiallyApplied class trick, we'd have to specify the error and return types:
ZIO.serviceWith[Foo, Nothing, Int](_.foo(int))
So it's just a slight QOL improvement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha. In an alternative universe, would an alternative Scala allow something like this?
def serviceWith[Service][E, A](f: Service => ZIO[Has[Service], E, A])
If so, has such syntax been proposed with the keepers of Scala?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha. In an alternative universe, would an alternative Scala allow something like this?
def serviceWith[Service][E, A](f: Service => ZIO[Has[Service], E, A])
If so, has such syntax been proposed with the keepers of Scala?
In scala3 you have named type arguments
If I'm not mistaken, it should allow you to do something like:
ZIO.serviceWith[Service = Foo](_.foo(int))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ivanpagano That's amazing! I had no idea.
Co-authored-by: Balazs Zagyvai <415627+zagyi@users.noreply.github.com>
serviceWith
is merely a terser variant ofaccessM
, that takes care of the_.get
behind the scenes.