8000 ZIO 2.0: Rename Halt To FailCause by adamgfraser · Pull Request #5239 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ZIO 2.0: Rename Halt To FailCause #5239

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

Merged
merged 2 commits into from
Jun 25, 2021
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
16 changes: 8 additions & 8 deletions core-tests/shared/src/test/scala/zio/ZIOLazinessSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ object ZIOLazinessSpec extends ZIOBaseSpec {
testM("dieMessage")(assertLazy(IO.dieMessage)),
testM("done")(assertLazy(IO.done)),
testM("fail")(assertLazy(IO.fail)),
testM("failCause")(assertLazy(IO.failCause)),
testM("fromEither")(assertLazy(IO.fromEither)),
testM("fromFiber")(assertLazy(IO.fromFiber)),
testM("fromOption")(assertLazy(IO.fromOption)),
testM("fromTry")(assertLazy(IO.fromTry)),
testM("getOrFailUnit")(assertLazy(IO.getOrFailUnit)),
testM("halt")(assertLazy(IO.halt)),
testM("interruptAs")(assertLazy(IO.interruptAs)),
testM("left")(assertLazy(IO.left)),
testM("lock")(assertLazy(IO.lock)),
Expand All @@ -35,8 +35,8 @@ object ZIOLazinessSpec extends ZIOBaseSpec {
testM("dieMessage")(assertLazy(Managed.dieMessage)),
testM("done")(assertLazy(Managed.done)),
testM("fail")(assertLazy(Managed.fail)),
testM("failCause")(assertLazy(Managed.failCause)),
testM("fromEither")(assertLazy(Managed.fromEither)),
testM("halt")(assertLazy(Managed.halt)),
testM("require")(assertLazy(Managed.require)),
testM("succeed")(assertLazy(Managed.succeed))
),
Expand All @@ -45,11 +45,11 @@ object ZIOLazinessSpec extends ZIOBaseSpec {
testM("dieMessage")(assertLazy(RIO.dieMessage)),
testM("done")(assertLazy(RIO.done)),
testM("fail")(assertLazy(RIO.fail)),
testM("failCause")(assertLazy(RIO.failCause)),
testM("fromEither")(assertLazy(RIO.fromEither)),
testM("fromFiber")(assertLazy(RIO.fromFiber)),
testM("fromTry")(assertLazy(RIO.fromTry)),
testM("getOrFail")(assertLazy(RIO.getOrFail)),
testM("halt")(assertLazy(RIO.halt)),
testM("interruptAs")(assertLazy(RIO.interruptAs)),
testM("left")(assertLazy(RIO.left)),
testM("lock")(assertLazy(RIO.lock)),
Expand All @@ -65,10 +65,10 @@ object ZIOLazinessSpec extends ZIOBaseSpec {
testM("dieMessage")(assertLazy(Task.dieMessage)),
testM("done")(assertLazy(Task.done)),
testM("fail")(assertLazy(Task.fail)),
testM("failCause")(assertLazy(Task.failCause)),
testM("fromEither")(assertLazy(Task.fromEither)),
testM("fromFiber")(assertLazy(Task.fromFiber)),
testM("fromTry")(assertLazy(Task.fromTry)),
testM("halt")(assertLazy(Task.halt)),
testM("interruptAs")(assertLazy(Task.interruptAs)),
testM("left")(assertLazy(Task.left)),
testM("lock")(assertLazy(Task.lock)),
Expand All @@ -81,9 +81,9 @@ object ZIOLazinessSpec extends ZIOBaseSpec {
testM("die")(assertLazy(UIO.die)),
testM("dieMessage")(assertLazy(UIO.dieMessage)),
testM("done")(assertLazy(UIO.done)),
testM("failCause")(assertLazy(UIO.failCause)),
testM("fromEither")(assertLazy(UIO.fromEither)),
testM("fromFiber")(assertLazy(UIO.fromFiber)),
testM("halt")(assertLazy(UIO.halt)),
testM("interruptAs")(assertLazy(UIO.interruptAs)),
testM("left")(assertLazy(UIO.left)),
testM("lock")(assertLazy(UIO.lock)),
Expand All @@ -95,9 +95,9 @@ object ZIOLazinessSpec extends ZIOBaseSpec {
testM("die")(assertLazy(URIO.die)),
testM("dieMessage")(assertLazy(URIO.dieMessage)),
testM("done")(assertLazy(URIO.done)),
testM("failCause")(assertLazy(URIO.failCause)),
testM("fromEither")(assertLazy(URIO.fromEither)),
testM("fromFiber")(assertLazy(URIO.fromFiber)),
testM("halt")(assertLazy(URIO.halt)),
testM("interruptAs")(assertLazy(URIO.interruptAs)),
testM("left")(assertLazy(URIO.left)),
testM("lock")(assertLazy(URIO.lock)),
Expand All @@ -112,8 +112,8 @@ object ZIOLazinessSpec extends ZIOBaseSpec {
testM("dieMessage")(assertLazy(ZManaged.dieMessage)),
testM("done")(assertLazy(ZManaged.done)),
testM("fail")(assertLazy(ZManaged.fail)),
testM("failCause")(assertLazy(ZManaged.failCause)),
testM("fromEither")(assertLazy(ZManaged.fromEither)),
testM("halt")(assertLazy(ZManaged.halt)),
testM("interruptAs")(assertLazy(ZManaged.interruptAs)),
testM("require")(assertLazy(ZManaged.require)),
testM("succeed")(assertLazy(ZManaged.succeed))
Expand All @@ -123,12 +123,12 @@ object ZIOLazinessSpec extends ZIOBaseSpec {
testM("dieMessage")(assertLazy(ZIO.dieMessage)),
testM("done")(assertLazy(ZIO.done)),
testM("fail")(assertLazy(ZIO.fail)),
testM("failCause")(assertLazy(ZIO.failCause)),
testM("fromEither")(assertLazy(ZIO.fromEither)),
testM("fromFiber")(assertLazy(ZIO.fromFiber)),
testM("fromOption")(assertLazy(ZIO.fromOption)),
testM("fromTry")(assertLazy(ZIO.fromTry)),
testM("getOrFailUnit")(assertLazy(ZIO.getOrFailUnit)),
testM("halt")(assertLazy(ZIO.halt)),
testM("interruptAs")(assertLazy(ZIO.interruptAs)),
testM("left")(assertLazy(ZIO.left)),
testM("lock")(assertLazy(ZIO.lock)),
Expand Down
16 changes: 8 additions & 8 deletions core-tests/shared/src/test/scala/zio/ZIOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ object ZIOSpec extends ZIOBaseSpec {
assert(_)(isTrue)
)
},
testM("halts if cause doesn't match") {
testM("fails if cause doesn't match") {
ZIO.fiberId.flatMap { fiberId =>
ZIO.interrupt.catchSomeCause {
case c if (!c.interrupted) => ZIO.succeed(true)
Expand Down Expand Up @@ -1466,8 +1466,8 @@ object ZIOSpec extends ZIOBaseSpec {
implicit val canFail = CanFail
for {
plain <- (ZIO.die(ex) <> IO.unit).exit
both <- (ZIO.halt(Cause.Both(interrupt(fiberId), die(ex))) <> IO.unit).exit
thn <- (ZIO.halt(Cause.Then(interrupt(fiberId), die(ex))) <> IO.unit).exit
both <- (ZIO.failCause(Cause.Both(interrupt(fiberId), die(ex))) <> IO.unit).exit
thn <- (ZIO.failCause(Cause.Then(interrupt(fiberId), die(ex))) <> IO.unit).exit
fail <- (ZIO.fail(ex) <> IO.unit).exit
} yield assert(plain)(dies(equalTo(ex))) &&
assert(both)(dies(equalTo(ex))) &&
Expand Down Expand Up @@ -1496,7 +1496,7 @@ object ZIOSpec extends ZIOBaseSpec {
val smallInts = Gen.int(0, 100)
val causes = Gen.causes(smallInts, Gen.throwable)
val successes = Gen.successes(smallInts)
val exits = Gen.either(causes, successes).map(_.fold(Exit.halt, Exit.succeed))
val exits = Gen.either(causes, successes).map(_.fold(Exit.failCause, Exit.succeed))
checkM(exits, exits, exits) { (exit1, exit2, exit3) =>
val zio1 = ZIO.done(exit1)
val zio2 = ZIO.done(exit2)
Expand Down Expand Up @@ -2096,7 +2096,7 @@ object ZIOSpec extends ZIOBaseSpec {
Cause.die(InterruptCause2) ++
Cause.die(InterruptCause3)

assertM(io.exit)(equalTo(Exit.halt(expectedCause)))
assertM(io.exit)(equalTo(Exit.failCause(expectedCause)))
} @@ zioTag(errors),
testM("catch failing finalizers with terminate") {
val io = IO
Expand All @@ -2110,7 +2110,7 @@ object ZIOSpec extends ZIOBaseSpec {
Cause.die(InterruptCause2) ++
Cause.die(InterruptCause3)

assertM(io.exit)(equalTo(Exit.halt(expectedCause)))
assertM(io.exit)(equalTo(Exit.failCause(expectedCause)))
} @@ zioTag(errors),
testM("run preserves interruption status") {
for {
Expand Down Expand Up @@ -2138,7 +2138,7 @@ object ZIOSpec extends ZIOBaseSpec {
},
testM("timeout a long computation with a cause") {
val cause = Cause.die(new Error("BOOM"))
val io = (Clock.sleep(5.seconds) *> IO.succeed(true)).timeoutHalt(cause)(10.millis)
val io = (Clock.sleep(5.seconds) *> IO.succeed(true)).timeoutFailCause(cause)(10.millis)
assertM(Live.live(io.sandbox.flip))(equalTo(cause))
},
testM("timeout repetition of uninterruptible effect") {
Expand Down Expand Up @@ -3382,7 +3382,7 @@ object ZIOSpec extends ZIOBaseSpec {
zio.foldCauseZIO(ZIO.succeed(_), ZIO.fail)
checkM(causes) { c =>
for {
result <- cause(ZIO.halt(c).sandbox.mapErrorCause(e => e.untraced).unsandbox)
result <- cause(ZIO.failCause(c).sandbox.mapErrorCause(e => e.untraced).unsandbox)
} yield assert(result)(equalTo(c)) &&
assert(result.prettyPrint)(equalTo(c.prettyPrint))
}
Expand Down
22 changes: 15 additions & 7 deletions core/shared/src/main/scala/zio/Exit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ sealed abstract class Exit[+E, +A] extends Product with Serializable { self =>
* returns the result in a new `Exit`.
*/
final def foreach[R, E1 >: E, B](f: A => ZIO[R, E1, B]): ZIO[R, Nothing, Exit[E1, B]] =
fold(c => ZIO.succeedNow(halt(c)), a => f(a).exit)
fold(c => ZIO.succeedNow(failCause(c)), a => f(a).exit)

/**
* Retrieves the `A` if succeeded, or else returns the specified default `A`.
Expand Down Expand Up @@ -165,7 +165,7 @@ sealed abstract class Exit[+E, +A] extends Product with Serializable { self =>
final def mapError[E1](f: E => E1): Exit[E1, A] =
self match {
case e @ Success(_) => e
case Failure(c) => halt(c.map(f))
case Failure(c) => failCause(c.map(f))
}

/**
Expand Down Expand Up @@ -249,7 +249,7 @@ sealed abstract class Exit[+E, +A] extends Product with Serializable { self =>
): Exit[E1, C] =
(self, that) match {
case (Success(a), Success(b)) => Exit.succeed(f(a, b))
case (Failure(l), Failure(r)) => Exit.halt(g(l, r))
case (Failure(l), Failure(r)) => Exit.failCause(g(l, r))
case (e @ Failure(_), _) => e
case (_, e @ Failure(_)) => e
}
Expand All @@ -260,7 +260,8 @@ object Exit extends Serializable {
final case class Success[+A](value: A) extends Exit[Nothing, A]
final case class Failure[+E](cause: Cause[E]) extends Exit[E, Nothing]

def interrupt(id: Fiber.Id): Exit[Nothing, Nothing] = halt(Cause.interrupt(id))
def interrupt(id: Fiber.Id): Exit[Nothing, Nothing] =
failCause(Cause.interrupt(id))

def collectAll[E, A](exits: Iterable[Exit[E, A]]): Option[Exit[E, List[A]]] =
exits.headOption.map { head =>
Expand All @@ -278,9 +279,14 @@ object Exit extends Serializable {
.map(_.reverse)
}

def die(t: Throwable): Exit[Nothing, Nothing] = halt(Cause.die(t))
def die(t: Throwable): Exit[Nothing, Nothing] =
failCause(Cause.die(t))

def fail[E](error: E): Exit[E, Nothing] = halt(Cause.fail(error))
def fail[E](error: E): Exit[E, Nothing] =
failCause(Cause.fail(error))

def failCause[E](cause: Cause[E]): Exit[E, Nothing] =
Failure(cause)

def flatten[E, A](exit: Exit[E, Exit[E, A]]): Exit[E, A] =
exit.flatMap(identity)
Expand All @@ -297,7 +303,9 @@ object Exit extends Serializable {
case scala.util.Failure(t) => fail(t)
}

def halt[E](cause: Cause[E]): Exit[E, Nothing] = Failure(cause)
@deprecated("use failCause", "2.0.0")
def halt[E](cause: Cause[E]): Exit[E, Nothing] =
failCause(cause)

def succeed[A](a: A): Exit[Nothing, A] = Success(a)

Expand Down
12 changes: 10 additions & 2 deletions core/shared/src/main/scala/zio/Fiber.scala
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ sealed abstract class Fiber[+E, +A] { self =>
* @return `Fiber[E, B]` The continued fiber.
*/
final def mapFiber[E1 >: E, B](f: A => Fiber[E1, B]): UIO[Fiber[E1, B]] =
self.await.map(_.fold(Fiber.halt(_), f))
self.await.map(_.fold(Fiber.failCause(_), f))

/**
* Effectually maps over the value the fiber computes.
Expand Down Expand Up @@ -654,6 +654,12 @@ object Fiber extends FiberPlatformSpecific {
*/
def fail[E](e: E): Fiber.Synthetic[E, Nothing] = done(Exit.fail(e))

/**
* Creates a `Fiber` that has already failed with the specified cause.
*/
def failCause[E](cause: Cause[E]): Fiber.Synthetic[E, Nothing] =
done(Exit.failCause(cause))

/**
* A `FiberRef` that stores the name of the fiber, which defaults to `None`.
*/
Expand Down Expand Up @@ -713,7 +719,9 @@ object Fiber extends FiberPlatformSpecific {
/**
* Creates a `Fiber` that is halted with the specified cause.
*/
def halt[E](cause: Cause[E]): Fiber.Synthetic[E, Nothing] = done(Exit.halt(cause))
@deprecated("use failCause", "2.0.0")
def halt[E](cause: Cause[E]): Fiber.Synthetic[E, Nothing] =
failCause(cause)

/**
* Interrupts all fibers, awaiting their interruption.
Expand Down
17 changes: 16 additions & 1 deletion core/shared/src/main/scala/zio/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,18 @@ object IO {
*/
def fail[E](error: => E): IO[E, Nothing] = ZIO.fail(error)

/**
* @see See [[zio.ZIO.failCause]]
*/
def failCause[E](cause: => Cause[E]): IO[E, Nothing] =
ZIO.failCause(cause)

/**
* @see See [[zio.ZIO.failCauseWith]]
*/
def failCauseWith[E](function: (() => ZTrace) => Cause[E]): IO[E, Nothing] =
ZIO.failCauseWith(function)

/**
* @see [[zio.ZIO.fiberId]]
*/
Expand Down Expand Up @@ -844,11 +856,14 @@ object IO {
/**
* @see See [[zio.ZIO.halt]]
*/
def halt[E](cause: => Cause[E]): IO[E, Nothing] = ZIO.halt(cause)
@deprecated("use failCause", "2.0.0")
def halt[E](cause: => Cause[E]): IO[E, Nothing] =
ZIO.halt(cause)

/**
* @see See [[zio.ZIO.haltWith]]
*/
@deprecated("use failCauseWith", "2.0.0")
def haltWith[E](function: (() => ZTrace) => Cause[E]): IO[E, Nothing] =
ZIO.haltWith(function)

Expand Down
11 changes: 10 additions & 1 deletion core/shared/src/main/scala/zio/Promise.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,20 @@ final class Promise[E, A] private (
*/
def fail(e: E): UIO[Boolean] = completeWith(IO.fail(e))

/**
* Fails the promise with the specified cause, which will be propagated to all
* fibers waiting on the value of the promise.
*/
def failCause(e: Cause[E]): UIO[Boolean] =
completeWith(IO.failCause(e))

/**
* Halts the promise with the specified cause, which will be propagated to all
* fibers waiting on the value of the promise.
*/
def halt(e: Cause[E]): UIO[Boolean] = completeWith(IO.halt(e))
@deprecated("use failCause", "2.0.0")
def halt(e: Cause[E]): UIO[Boolean] =
failCause(e)

/**
* Completes the promise with interruption. This will interrupt all fibers
Expand Down
17 changes: 16 additions & 1 deletion core/shared/src/main/scala/zio/RIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,18 @@ object RIO {
*/
def fail(error: => Throwable): Task[Nothing] = ZIO.fail(error)

/**
* @see See [[zio.ZIO.failCause]]
*/
def failCause(cause: => Cause[Throwable]): Task[Nothing] =
ZIO.failCause(cause)

/**
* @see See [[zio.ZIO.failCauseWith]]
*/
def failCauseWith[R](function: (() => ZTrace) => Cause[Throwable]): RIO[R, Nothing] =
ZIO.failCauseWith(function)

/**
* @see [[zio.ZIO.fiberId]]
*/
Expand Down Expand Up @@ -875,11 +887,14 @@ object RIO {
/**
* @see See [[zio.ZIO.halt]]
*/
def halt(cause: => Cause[Throwable]): Task[Nothing] = ZIO.halt(cause)
@deprecated("use failCause", "2.0.0")
def halt(cause: => Cause[Throwable]): Task[Nothing] =
ZIO.halt(cause)

/**
* @see See [[zio.ZIO.haltWith]]
*/
@deprecated("use failCauseWith", "2.0.0")
def haltWith[R](function: (() => ZTrace) => Cause[Throwable]): RIO[R, Nothing] =
ZIO.haltWith(function)

Expand Down
17 changes: 16 additions & 1 deletion core/shared/src/main/scala/zio/Task.scala
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,18 @@ object Task extends TaskPlatformSpecific {
*/
def fail(error: => Throwable): Task[Nothing] = ZIO.fail(error)

/**
* @see See [[zio.ZIO.failCause]]
*/
def failCause(cause: => Cause[Throwable]): Task[Nothing] =
ZIO.failCause(cause)

/**
* @see See [[zio.ZIO.failCauseWith]]
*/
def failCauseWith[E <: Throwable](function: (() => ZTrace) => Cause[E]): Task[Nothing] =
ZIO.failCauseWith(function)

/**
* @see [[zio.ZIO.fiberId]]
*/
Expand Down Expand Up @@ -832,11 +844,14 @@ object Task extends TaskPlatformSpecific {
/**
* @see See [[zio.ZIO.halt]]
*/
def halt(cause: => Cause[Throwable]): Task[Nothing] = ZIO.halt(cause)
@deprecated("use failCause", "2.0.0")
def halt(cause: => Cause[Throwable]): Task[Nothing] =
ZIO.halt(cause)

/**
* @see See [[zio.ZIO.haltWith]]
*/
@deprecated("use failCauseWith", "2.0.0")
def haltWith[E <: Throwable](function: (() => ZTrace) => Cause[E]): Task[Nothing] =
ZIO.haltWith(function)

Expand Down
Loading
0