-
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
Conversation
8beceb6 to
2f4b01c
Compare
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.
| * Returns a successful effect if the value is 'Left', or fails with the error f. | |
| * Returns a successful effect if the value is `Left`, or fails with the error `f`. |
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.
Can we name f as e?
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 task as Task[Int]?
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
def leftOrFailException[B, C](
implicit ev: A <:< Either[B, C]
): ZIO[R, NoSuchElementException, B]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.
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.
Hmm why E1 >: NoSuchElementException rather than just returning ZIO[R, NoSuchElementException, B]? Since E1 doesn't show up in the function's parameters (other than the ev2 but that doesn't influence inference, I think), this might not infer properly (or worse inferred as Any)
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.
2 problems here - this is throwing in map and will cause a defect (rather than a typed exception), and projections are deprecated in 2.13 if I am not mistaken.
Can you fold/pattern match on the Either instead?
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.
I agree on both points.
I am struggling with the fold/pattern matching on the '*exception' methods and opened this PR to get feedback on how to do this properly(please see my comment above).
would love some help here if possible :)
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.
Sorry, I take back what I said about the E1 - the bounds are necessary to unify NoSuchElementException with the existing E.
This should work without right.get:
/**
* Returns a successful effect if the value is `Left`, or fails with a [[scala.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)
)
)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.
Same as with leftOrFailException
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.
Same comments as with leftOrFailException
8010d16 to
f7cf6b4
Compare
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.
This method does almost the same as ZIO#get, do we really need both?
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.
@evis, I agree with you.
I think the difference is that: someOrFailException gives you an exception in case of a failure while .get just return the unit value.
what do you think about expressing .get using someOrFailException? does that make sense?
Something like
final def get[E1 >: E, B](implicit ev: A <:< Option[B]): ZIO[R, Unit, B] =
self.someOrFail(()).mapError(_ => ())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.
Good idea @dorsev
f7cf6b4 to
b6d79cd
Compare
|
Thank you very much for the help @iravid !!! much appreciated! A thought: Should I add a custom "message" for every "NoSuchElementException" like in |
|
Yeah, a custom message would be a nice touch 👍 |
b6d79cd to
fd47eef
Compare
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.
Oh, I forgot this had the E =:= Nothing constraint. I think we should leave this method alone; we're discarding an existing error here.
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.
| * Returns a successful effect if the value is `Right`, or fails with a [[scala.NoSuchElementException]]. | |
| * Returns a successful effect if the value is `Right`, or fails with a [[java.util.NoSuchElementException]]. |
|
Two small comments and this is good to go. |
dfb2dc4 to
b3b5acd
Compare
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.
Excellent work!
|
@dorsev Thank you for your contribution! |
* Add someOrFail methods to ZIO trait * Add rightOrFail methods to ZIO trait * Add leftOrFail methods to ZIO trait * Add option and either helper method to ZIO object - Fix zio#1160
Resolves #1160.