scalazio

ZIO, Provide environment from Effect


Starting with ZIO I am trying to convert an existing application to a pure functional one. This application depends on an Http and an Ftp client. They are both configured from a configuration file and optional command line arguments.

The current flow parse the arguments, then read the configuration file and merge both. The resulting Configuration is passed to some components that need the Http and Ftp client.

Now, if I want to convert that to ZIO, I can naively create such flow :

val flow = for {
  options <- ZIO.succeed("Options")
  configs <- ZIO.succeed(s"Config + $options")
  result  <- ZIO.succeed("...") // This require the "configs"
} yield ()

Unfortunately, I cannot provide the configs since it is produced into the for comprehension itself.

My question is: How can we provide the result of one effect as environment ?


Solution

  • You can do .provide(configs), why not?

    More generally you can do io1.flatMap(io2.provide) to run effect io1 and then provide the result to another effect io2. There's even a shortcut for this: io1 >>> io2.