zio

ZIO

ZIO effect system patterns for building type-safe, asynchronous, and concurrent Scala applications

Details

Language / Topic
scalaScala
Category
framework
Compatible Frameworks
zio

Rules

balanced
- Model effects as `ZIO[R, E, A]` with the `R` environment, `E` typed error, and `A` success value — never use `ZIO[Any, Throwable, A]` when you can be more specific.
- Use `ZLayer` for dependency injection — compose layers with `++` (horizontal) and `>>>` (vertical/sequential) to build the application's dependency graph.
- Use `ZIO.attempt` to wrap side-effecting Java/Scala code and `ZIO.succeed` for pure values — never use `throw` inside a `ZIO` block.
- Define service interfaces as `trait MyService` with a companion `ZLayer` in the companion object — expose via `ZIO.serviceWithZIO[MyService](_.doWork())`.
- Use `ZIO.scoped` and `ZIO.acquireRelease` for resource lifecycle management — release actions run even if the effect is interrupted or fails.
- Use `ZIO.foreach` and `ZIO.collectAllPar` for bulk operations — prefer `collectAllPar` for independent effects and `foreach` for sequential dependent ones.
- Model domain errors as a sealed `AppError` trait and use `ZIO[R, AppError, A]` — avoid `ZIO[R, Throwable, A]` except at integration boundaries.