Skip to content

Latest commit

 

History

History
103 lines (70 loc) · 2.45 KB

README.md

File metadata and controls

103 lines (70 loc) · 2.45 KB

e-zio

This module provides aliases EIO and REIO for ZIO. They fix the E type parameter of ZIO[R, E, A] to E so you can use all ZIO and e goodness together.

Installation

If you use SBT, add following to your build.sbt:

libraryDependencies += "dev.akif" %% "e-zio" % "3.0.1"

If you use Maven, add following to your pom.xml:

<dependencies>
  <dependency>
    <groupId>dev.akif</groupId>
    <artifactId>e-zio_3</artifactId>
    <version>3.0.1</version>
  </dependency>
</dependencies>

If you use Gradle, add following to your project's build.gradle:

dependencies {
  implementation('dev.akif:e-zio_3:3.0.1')
}

Contents

Below are some details and examples of e-zio's content. For more, please check corresponding automated tests and e-zio's documentation.

To get started, add following import which will cover all your needs:

import e.ezio.*

1. EIO

import e.ezio.*
import e.scala.*

// From E
E.name("test-error").message("Test").toEIO[String]

// From value
42.toEIO

// From failed EOr
E.code(1).toEOr[String].toEIO

// From successful EOr
"test".toEOr.toEIO

def divide(a: Int, b: Int): EIO[Int] =
  if b == 0 then E.name("divideError").message("Cannot divide by 0!").data("input", a.toString).toEIO[Int]
  else (a / b).toEIO

divide(4, 2)

divide(4, 0)

2. REIO

import e.ezio.*
import e.scala.*
import zio.*

// From E
E.name("test-error").message("Test").toREIO[Boolean, String]

// From value
42.toREIO[String]

// From failed EOr
E.code(1).toEOr[String].toREIO[Long]

// From successful EOr
"test".toEOr.toREIO[String]

class Divider:
  def divide(a: Int, b: Int): EIO[Int] =
      if b == 0 then E.name("divideError").message("Cannot divide by 0!").data("input", a.toString).toEIO[Int]
      else (a / b).toEIO

val divider: Divider = new Divider

def divide(a: Int, b: Int): REIO[Divider, Int] = REIO.serviceWithZIO[Divider](_.divide(a, b))

divide(4, 2).provide(ZLayer.succeed(divider))

divide(4, 0).provide(ZLayer.succeed(divider))