diff --git a/core-tests/shared/src/test/scala/zio/ZIOSpec.scala b/core-tests/shared/src/test/scala/zio/ZIOSpec.scala index 6443c9748db5..78f5b7050139 100644 --- a/core-tests/shared/src/test/scala/zio/ZIOSpec.scala +++ b/core-tests/shared/src/test/scala/zio/ZIOSpec.scala @@ -1208,6 +1208,14 @@ object ZIOSpec extends ZIOBaseSpec { assertM(task.run)(fails(isSome(equalTo(ex)))) } @@ zioTag(errors) ), + suite("negate")( + testM("on true returns false") { + assertM(ZIO.succeed(true).negate)(equalTo(false)) + }, + testM("on false returns true") { + assertM(ZIO.succeed(false).negate)(equalTo(true)) + } + ), suite("once")( testM("returns an effect that will only be executed once") { for { diff --git a/core/shared/src/main/scala/zio/IO.scala b/core/shared/src/main/scala/zio/IO.scala index 769a9f3473bd..ec951e1c19ab 100644 --- a/core/shared/src/main/scala/zio/IO.scala +++ b/core/shared/src/main/scala/zio/IO.scala @@ -675,6 +675,12 @@ object IO { */ val none: UIO[Option[Nothing]] = ZIO.none + /** + * @see See [[zio.ZIO.not]] + */ + def not[E](effect: IO[E, Boolean]): IO[E, Boolean] = + ZIO.not(effect) + /** * @see See [[zio.ZIO.partition]] */ diff --git a/core/shared/src/main/scala/zio/RIO.scala b/core/shared/src/main/scala/zio/RIO.scala index fcc3771c3f01..c1d8b42524f5 100644 --- a/core/shared/src/main/scala/zio/RIO.scala +++ b/core/shared/src/main/scala/zio/RIO.scala @@ -709,6 +709,12 @@ object RIO { */ val none: UIO[Option[Nothing]] = ZIO.none + /** + * @see See [[zio.ZIO.not]] + */ + def not[R](effect: RIO[R, Boolean]): RIO[R, Boolean] = + ZIO.not(effect) + /** * @see See [[zio.ZIO.partition]] */ diff --git a/core/shared/src/main/scala/zio/Task.scala b/core/shared/src/main/scala/zio/Task.scala index 196cf9ea8db1..ce51b79e9a12 100644 --- a/core/shared/src/main/scala/zio/Task.scala +++ b/core/shared/src/main/scala/zio/Task.scala @@ -670,6 +670,12 @@ object Task extends TaskPlatformSpecific { */ val none: Task[Option[Nothing]] = ZIO.none + /** + * @see See [[zio.ZIO.not]] + */ + def not(effect: Task[Boolean]): Task[Boolean] = + ZIO.not(effect) + /** * @see See [[zio.ZIO.partition]] */ diff --git a/core/shared/src/main/scala/zio/UIO.scala b/core/shared/src/main/scala/zio/UIO.scala index 75b633c79c2a..9a4f41d6c78d 100644 --- a/core/shared/src/main/scala/zio/UIO.scala +++ b/core/shared/src/main/scala/zio/UIO.scala @@ -602,6 +602,12 @@ object UIO { */ val never: UIO[Nothing] = ZIO.never + /** + * @see See [[zio.ZIO.not]] + */ + def not(effect: UIO[Boolean]): UIO[Boolean] = + ZIO.not(effect) + /** * @see See [[zio.ZIO.raceAll]] */ diff --git a/core/shared/src/main/scala/zio/URIO.scala b/core/shared/src/main/scala/zio/URIO.scala index c7c6c9c290f4..34ee2c0beb67 100644 --- a/core/shared/src/main/scala/zio/URIO.scala +++ b/core/shared/src/main/scala/zio/URIO.scala @@ -648,6 +648,12 @@ object URIO { */ val none: UIO[Option[Nothing]] = ZIO.none + /** + * @see See [[zio.ZIO.not]] + */ + def not[R](effect: URIO[R, Boolean]): URIO[R, Boolean] = + ZIO.not(effect) + /** * @see [[zio.ZIO.provide]] */ diff --git a/core/shared/src/main/scala/zio/ZIO.scala b/core/shared/src/main/scala/zio/ZIO.scala index a4f7724e8811..84909b4e1f74 100644 --- a/core/shared/src/main/scala/zio/ZIO.scala +++ b/core/shared/src/main/scala/zio/ZIO.scala @@ -909,6 +909,12 @@ sealed trait ZIO[-R, +E, +A] extends Serializable with ZIOPlatformSpecific[R, E, final def merge[A1 >: A](implicit ev1: E <:< A1, ev2: CanFail[E]): URIO[R, A1] = self.foldM(e => ZIO.succeedNow(ev1(e)), ZIO.succeedNow) + /** + * Returns a new effect where boolean value of this effect is negated. + */ + final def negate(implicit ev: A <:< Boolean): ZIO[R, E, Boolean] = + map(result => !result) + /** * Requires the option produced by this value to be `None`. */ @@ -3291,6 +3297,12 @@ object ZIO extends ZIOCompanionPlatformSpecific { if (cont(initial)) body(initial) *> loop_(inc(initial))(cont, inc)(body) else ZIO.unit + /** + * Returns a new effect where boolean value of this effect is negated. + */ + def not[R, E](effect: ZIO[R, E, Boolean]): ZIO[R, E, Boolean] = + effect.negate + /** * Sequentially zips the specified effects. Specialized version of mapN. */