-
Couldn't load subscription status.
- Fork 1.4k
Enhance support for option and either - Fix #1160 #1175
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -787,8 +787,50 @@ sealed trait ZIO[-R, +E, +A] extends Serializable { self => | |
|
|
||
| final def left[R1 <: R, C]: ZIO[Either[R1, C], E, Either[A, C]] = self +++ ZIO.identity[C] | ||
|
|
||
| /** | ||
| * Returns a successful effect if the value is `Left`, or fails with the error e. | ||
| */ | ||
| def leftOrFail[B, C, E1 >: E](e: E1)(implicit ev: A <:< Either[B, C]): ZIO[R, E1, B] = | ||
| self.flatMap(ev(_) match { | ||
| case Right(_) => ZIO.fail(e) | ||
| case Left(value) => ZIO.succeed(value) | ||
| }) | ||
|
|
||
| /** | ||
| * Returns a successful effect if the value is `Left`, or fails with a [[java.util.NoSuchElementException]]. | ||
| */ | ||
| def leftOrFailException[B, C, E1 >: NoSuchElementException]( | ||
|
||
| implicit ev: A <:< Either[B, C], | ||
| ev2: E <:< E1 | ||
| ): ZIO[R, E1, B] = | ||
| self.foldM( | ||
| e => ZIO.fail(ev2(e)), | ||
| a => ev(a).fold(ZIO.succeed(_), _ => ZIO.fail(new NoSuchElementException("Either.left.get on Right"))) | ||
| ) | ||
|
|
||
| final def right[R1 <: R, C]: ZIO[Either[C, R1], E, Either[C, A]] = ZIO.identity[C] +++ self | ||
|
|
||
| /** | ||
| * Returns a successful effect if the value is `Right`, or fails with the given error 'e'. | ||
| */ | ||
| def rightOrFail[B, C, E1 >: E](e: E1)(implicit ev: A <:< Either[B, C]): ZIO[R, E1, C] = | ||
| self.flatMap(ev(_) match { | ||
| case Right(value) => ZIO.succeed(value) | ||
| case Left(_) => ZIO.fail(e) | ||
| }) | ||
|
|
||
| /** | ||
| * Returns a successful effect if the value is `Right`, or fails with a [[java.util.NoSuchElementException]]. | ||
| */ | ||
| def rightOrFailException[B, C, E1 >: NoSuchElementException]( | ||
|
||
| implicit ev: A <:< Either[B, C], | ||
| ev2: E <:< E1 | ||
| ): ZIO[R, E1, C] = | ||
| self.foldM( | ||
| e => ZIO.fail(ev2(e)), | ||
| a => ev(a).fold(_ => ZIO.fail(new NoSuchElementException("Either.right.get on Left")), ZIO.succeed(_)) | ||
| ) | ||
|
|
||
| /** | ||
| * A variant of `flatMap` that ignores the value produced by this effect. | ||
| */ | ||
|
|
@@ -1108,6 +1150,24 @@ sealed trait ZIO[-R, +E, +A] extends Serializable { self => | |
| */ | ||
| final def sandbox: ZIO[R, Cause[E], A] = foldCauseM(ZIO.fail, ZIO.succeed) | ||
|
|
||
| /** | ||
| * Extracts the optional value, or fails with the given error 'e'. | ||
| */ | ||
| def someOrFail[B, E1 >: E](e: E1)(implicit ev: A <:< Option[B]): ZIO[R, E1, B] = | ||
| self.flatMap(ev(_) match { | ||
| case Some(value) => ZIO.succeed(value) | ||
| case None => ZIO.fail(e) | ||
| }) | ||
|
|
||
| /** | ||
| * Extracts the optional value, or fails with a [[java.util.NoSuchElementException]] | ||
| */ | ||
| def someOrFailException[B, E1 >: NoSuchElementException](implicit ev: A <:< Option[B], ev2: E <:< E1): ZIO[R, E1, B] = | ||
|
||
| self.foldM(e => ZIO.fail(ev2(e)), ev(_) match { | ||
| case Some(value) => ZIO.succeed(value) | ||
| case None => ZIO.fail(new NoSuchElementException("None.get")) | ||
| }) | ||
|
|
||
| /** | ||
| * The inverse operation to `sandbox`. Submerges the full cause of failure. | ||
| */ | ||
|
|
@@ -1368,6 +1428,11 @@ private[zio] trait ZIOFunctions extends Serializable { | |
| */ | ||
| final val interrupt: UIO[Nothing] = haltWith(trace => Cause.Traced(Cause.Interrupt, trace())) | ||
|
|
||
| /** | ||
| * Returns an effect with the empty value. | ||
| */ | ||
| final val none: UIO[Option[Nothing]] = succeed(None) | ||
|
|
||
| /** | ||
| * Returns a effect that will never produce anything. The moral | ||
| * equivalent of `while(true) {}`, only without the wasted CPU cycles. | ||
|
|
@@ -1428,6 +1493,11 @@ private[zio] trait ZIOFunctions extends Serializable { | |
| final def forkAll_[R >: LowerR, E <: UpperE, A](as: Iterable[ZIO[R, E, A]]): ZIO[R, Nothing, Unit] = | ||
| as.foldRight[ZIO[R, Nothing, Unit]](ZIO.unit)(_.fork *> _) | ||
|
|
||
| /** | ||
| * Returns an effect with the value on the left part. | ||
| */ | ||
| final def left[A](a: A): UIO[Either[A, Nothing]] = succeed(Left(a)) | ||
|
|
||
| /** | ||
| * Returns an effect from a [[zio.Exit]] value. | ||
| */ | ||
|
|
@@ -1436,6 +1506,11 @@ private[zio] trait ZIOFunctions extends Serializable { | |
| case Exit.Failure(cause) => halt(cause) | ||
| } | ||
|
|
||
| /** | ||
| * Returns an effect with the optional value. | ||
| */ | ||
| def some[A](a: A): UIO[Option[A]] = succeed(Some(a)) | ||
|
|
||
| /** | ||
| * Enables supervision for this effect. This will cause fibers forked by | ||
| * this effect to be tracked and will enable their inspection via [[ZIO.children]]. | ||
|
|
@@ -1669,6 +1744,11 @@ private[zio] trait ZIOFunctions extends Serializable { | |
| final def require[E <: UpperE, A](error: E): IO[E, Option[A]] => IO[E, A] = | ||
| (io: IO[E, Option[A]]) => io.flatMap(_.fold[IO[E, A]](fail[E](error))(succeed[A])) | ||
|
|
||
| /** | ||
| * Returns an effect with the value on the right part. | ||
| */ | ||
| def right[B](b: B): UIO[Either[Nothing, B]] = succeed(Right(b)) | ||
|
|
||
| /** | ||
| * When this effect represents acquisition of a resource (for example, | ||
| * opening a file, launching a thread, etc.), `bracket` can be used to ensure | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work without annotating
taskasTask[Int]?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No.
The return type by the compiler is
[B, C, E1 >:NoSuchElementException]zio.ZIO[Any,E1,B]. 🤔also, changing the signature to
does not work too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I tested and it looks like it infers properly - scalac says it's a
ZIO[Any, NoSuchElementException, Nothing]which is accurate.