Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ZIO.ignore #8105

Merged
merged 2 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion core-tests/shared/src/test/scala/zio/ZIOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4289,7 +4289,23 @@ object ZIOSpec extends ZIOBaseSpec {
for {
cause <- ZIO.fail(new RuntimeException("fail")).ensuring(ZIO.die(new RuntimeException("die"))).orDie.cause
} yield assertTrue(cause.size == 2)
}
},
suite("ignore")(
test("ignores successes") {
var evaluated = false
val workflow = ZIO.ignore { evaluated = true }
for {
_ <- workflow
} yield assertTrue(evaluated)
},
test("ignores failures") {
var evaluated = false
val workflow = ZIO.ignore { evaluated = true; throw new Exception("fail") }
for {
_ <- workflow
} yield assertTrue(evaluated)
}
)
)

def functionIOGen: Gen[Any, String => ZIO[Any, Throwable, Int]] =
Expand Down
21 changes: 21 additions & 0 deletions core/shared/src/main/scala-2/zio/ZIOCompanionVersionSpecific.scala
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,27 @@ trait ZIOCompanionVersionSpecific {
def attemptBlockingIO[A](effect: => A)(implicit trace: Trace): IO[IOException, A] =
attemptBlocking(effect).refineToOrDie[IOException]

/**
* Returns an effect that, when executed, will cautiously run the provided
* code, ignoring it success or failure.
*/
def ignore(code: => Any)(implicit trace: Trace): UIO[Unit] =
ZIO.suspendSucceed {
try {
code

Exit.unit
} catch {
case t: Throwable =>
ZIO.withFiberRuntime[Any, Nothing, Unit] { (fiberState, _) =>
if (!fiberState.isFatal(t)(Unsafe.unsafe))
Exit.unit
else
throw t
}
}
}

/**
* Returns an effect that models success with the specified value.
*/
Expand Down
23 changes: 23 additions & 0 deletions core/shared/src/main/scala-3/zio/ZIOCompanionVersionSpecific.scala
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,29 @@ trait ZIOCompanionVersionSpecific {
def attemptBlockingIO[A](effect: Unsafe ?=> A)(implicit trace: Trace): IO[IOException, A] =
attemptBlocking(effect).refineToOrDie[IOException]

/**
* Returns an effect that, when executed, will cautiously run the provided
* code, ignoring it success or failure.
*/
def ignore(code: Unsafe ?=> Any)(implicit trace: Trace): UIO[Unit] =
ZIO.suspendSucceed {
try {
given Unsafe = Unsafe.unsafe

code

Exit.unit
} catch {
case t: Throwable =>
ZIO.withFiberRuntime[Any, Nothing, Unit] { (fiberState, _) =>
if (!fiberState.isFatal(t)(Unsafe.unsafe))
Exit.unit
else
throw t
}
}
}

/**
* Returns an effect that models success with the specified value.
*/
Expand Down
Loading