Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@kitlangton
Copy link
Member

@kitlangton kitlangton commented Apr 10, 2021

serviceWith is merely a terser variant of accessM, that takes care of the _.get behind the scenes.

@kitlangton kitlangton requested a review from iravid as a code owner April 10, 2021 20:43
* def foo(int: Int) = ZIO.serviceWith[Foo](_.foo(int))
* }}}
*/
def serviceWith[Service]: ServiceWithPartiallyApplied[Service] =

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.

Copy link
Member Author

@kitlangton kitlangton Apr 11, 2021

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.

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?

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))

Copy link
Member Author

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 <[email protected]>
@adamgfraser adamgfraser merged commit cf0c90a into zio:master Apr 11, 2021
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.

5 participants