# Working with ZIO and http4s given a non-empty ZIO environment

**URL:** <https://softwaremill.community/t/working-with-zio-and-http4s-given-a-non-empty-zio-environment/65>\
**Category:** tapir\
**Created:** [January 2, 2023, 2:22pm UTC](https://softwaremill.community/t/working-with-zio-and-http4s-given-a-non-empty-zio-environment/65 "2023-01-02T14:22:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![beneyal](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/beneyal/32/57_2.png) [@beneyal](https://softwaremill.community/u/beneyal)\
**Post date:** [January 2, 2023, 2:22pm UTC](https://softwaremill.community/t/working-with-zio-and-http4s-given-a-non-empty-zio-environment/65/1 "2023-01-02T14:22:15Z")

</div>

Hello everyone,

I’m trying to create a simple server that has some dependencies and I’m following the instructions [here](https://tapir.softwaremill.com/en/latest/server/zio-http4s.html), replacing `*` with `_` as I’m using Scala 3 (I believe that’s correct, but not really sure). This is the code, with comments regarding the compiler errors.

```plaintext
object Http4sMain extends ZIOAppDefault {
  object endpoints {
    object root {
      val root: ZServerEndpoint[Any, Any] =
        endpoint.get.out(stringBody).serverLogicSuccess(_ => ZIO.succeed("empty"))

      val info: ZServerEndpoint[Indexing & Monitoring, Any] =
        endpoint.get.in("info").out(stringBody).serverLogicSuccess(_ => ZIO.succeed("info"))

      val health: ZServerEndpoint[Any, Any] =
        endpoint.get.in("health").serverLogicSuccess(_ => ZIO.unit)

      val all = List(info.widen[Env], health.widen[Env], root.widen[Env])
    }
  }

  type Env = MyConfig & Indexing & Monitoring
  // fails to compile with error "Type argument ZIO[Env, Throwable, ?]
  // does not have the same kind as its bound"
  val routes: HttpRoutes[RIO[Env, _]] = ZHttp4sServerInterpreter().from(endpoints.root.all).toRoutes

  def run = ZIO.executor.flatMap { executor =>
    BlazeServerBuilder[Task] // Neither Task nor RIO[Env, _] compile, probably because the error above
      .withExecutionContext(executor.asExecutionContext)
      .bindHttp(8000, "0.0.0.0")
      .withHttpApp(Router("/" -> routes).orNotFound)
      .resource
      .use { server =>
        for {
          _ <- Console.printLine(
            s"Server started at http://0.0.0.0:${server.address.getPort}. Press ENTER key to exit."
          )
          _ <- Console.readLine
        } yield ()
      }
  }
}

```

EDIT: Doing this:

```plaintext
type Effect[+A] = RIO[Env, A]

val routes: HttpRoutes[Effect] = ZHttp4sServerInterpreter().from(endpoints.root.all).toRoutes

```

and then using `Effect` as `F` for `BlazeServerBuilder` does compile, so this brings up two questions:

1. What did I do wrong when trying to mimic the kind projector in Scala 3?
2. How and where do I `.provide` the dependencies to my effects?

Any help is greatly appreciated! 🙏

---

<div class="post-metadata">

**Author:** ![adamw](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/adamw/32/28_2.png) [@adamw](https://softwaremill.community/u/adamw)\
**Post date:** [January 3, 2023, 10:25am UTC](https://softwaremill.community/t/working-with-zio-and-http4s-given-a-non-empty-zio-environment/65/2 "2023-01-03T10:25:31Z")

</div>

According to the [migration guide](https://docs.scala-lang.org/scala3/guides/migration/plugin-kind-projector.html), indeed you should replace `*` with `_`. I don’t have a scala 3 + zio + tapir project at hand to verify, though.

As for providing dependencies, the `ZIO.executor.flatMap { ... }` should yield a ZIO effect with the environment requiring your dependencies, so that’s where you’d provide them. See [this example](https://github.com/softwaremill/tapir/blob/master/examples/src/main/scala/sttp/tapir/examples/ZioEnvExampleHttp4sServer.scala#L77). So I think this would be something like `ZIO.executor.flatMap { ... }.provide(...)`.

To debug the typechecking issue, can you maybe create a self-contained example project (e.g. with the help of [adopt-tapir](https://adopt-tapir.softwaremill.com), or just paste in a code snippet), so that I could debug locally?

---

<div class="post-metadata">

**Author:** ![beneyal](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/beneyal/32/57_2.png) [@beneyal](https://softwaremill.community/u/beneyal)\
**Post date:** [January 3, 2023, 1:03pm UTC](https://softwaremill.community/t/working-with-zio-and-http4s-given-a-non-empty-zio-environment/65/3 "2023-01-03T13:03:20Z")

</div>

Hi Adam,

I made a self-contained Scastie [here](https://scastie.scala-lang.org/o865BG82RbOK1x3pYuCSkA).

Thanks for the help 🙂

---

<div class="post-metadata">

**Author:** ![adamw](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/adamw/32/28_2.png) [@adamw](https://softwaremill.community/u/adamw)\
**Post date:** [January 3, 2023, 3:45pm UTC](https://softwaremill.community/t/working-with-zio-and-http4s-given-a-non-empty-zio-environment/65/4 "2023-01-03T15:45:18Z")

</div>

I have no idea why (maybe a Scala issue?) but the following works:

```auto
  type X[A] = RIO[Int, A]
  val routes: HttpRoutes[X] = ZHttp4sServerInterpreter().from(List(dummyEndpoint)).toRoutes

```

---

<div class="post-metadata">

**Author:** ![adamw](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/adamw/32/28_2.png) [@adamw](https://softwaremill.community/u/adamw)\
**Post date:** [January 3, 2023, 3:46pm UTC](https://softwaremill.community/t/working-with-zio-and-http4s-given-a-non-empty-zio-environment/65/5 "2023-01-03T15:46:12Z")

</div>

Maybe it would be good to report it somewhere in Dotty (if you do, please link back 🙂 ).  
We are returning a `HttpRoutes[RIO[R with Clock with Blocking, *]]` using the Scala 2 notation so this should carry over to Scala 3 I guess … but apparently it doesn’t

---

<div class="post-metadata">

**Author:** ![beneyal](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/beneyal/32/57_2.png) [@beneyal](https://softwaremill.community/u/beneyal)\
**Post date:** [January 3, 2023, 3:57pm UTC](https://softwaremill.community/t/working-with-zio-and-http4s-given-a-non-empty-zio-environment/65/6 "2023-01-03T15:57:26Z")

</div>

Thanks for taking a look! I’ll open an issue in Dotty when I can 🙂

---

<div class="post-metadata">

**Author:** ![beneyal](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/beneyal/32/57_2.png) [@beneyal](https://softwaremill.community/u/beneyal)\
**Post date:** [January 3, 2023, 9:53pm UTC](https://softwaremill.community/t/working-with-zio-and-http4s-given-a-non-empty-zio-environment/65/7 "2023-01-03T21:53:14Z")

</div>

After doing some more reading [here](https://docs.scala-lang.org/scala3/reference/changed-features/wildcards.html#migration-strategy), I added the `-Ykind-projector` compiler flag and now `RIO[Env, *]` works correctly in Scala 3.2.

I also tried adding the `.provide` where you said, and it works like a charm! Thanks! 🙂
