diff --git a/core-tests/shared/src/test/scala/zio/ZIOSpec.scala b/core-tests/shared/src/test/scala/zio/ZIOSpec.scala index dfccca6d4279..f707564a9a29 100644 --- a/core-tests/shared/src/test/scala/zio/ZIOSpec.scala +++ b/core-tests/shared/src/test/scala/zio/ZIOSpec.scala @@ -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]] = diff --git a/core/shared/src/main/scala-2/zio/ZIOCompanionVersionSpecific.scala b/core/shared/src/main/scala-2/zio/ZIOCompanionVersionSpecific.scala index 87200973a730..d12c9254917e 100644 --- a/core/shared/src/main/scala-2/zio/ZIOCompanionVersionSpecific.scala +++ b/core/shared/src/main/scala-2/zio/ZIOCompanionVersionSpecific.scala @@ -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. */ diff --git a/core/shared/src/main/scala-3/zio/ZIOCompanionVersionSpecific.scala b/core/shared/src/main/scala-3/zio/ZIOCompanionVersionSpecific.scala index 6784464dae38..6b683cd970cc 100644 --- a/core/shared/src/main/scala-3/zio/ZIOCompanionVersionSpecific.scala +++ b/core/shared/src/main/scala-3/zio/ZIOCompanionVersionSpecific.scala @@ -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. */