Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@dorsev
Copy link
Contributor

@dorsev dorsev commented Jul 12, 2019

Resolves #1160.

@dorsev dorsev force-pushed the enhance-support-for-option-either branch from 8beceb6 to 2f4b01c Compare July 12, 2019 18:38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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`.

Copy link
Member

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?

Copy link
Member

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]?

Copy link
Contributor Author

@dorsev dorsev Jul 12, 2019

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.

Copy link
Member

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.

Copy link
Member

@iravid iravid Jul 12, 2019

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)

Copy link
Member

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?

Copy link
Contributor Author

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 :)

Copy link
Member

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)
        )
    )

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as with leftOrFailException

Copy link
Member

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

@dorsev dorsev force-pushed the enhance-support-for-option-either branch 3 times, most recently from 8010d16 to f7cf6b4 Compare July 12, 2019 21:21
Copy link
Contributor

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?

Copy link
Contributor Author

@dorsev dorsev Jul 13, 2019

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(_ => ())

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea @dorsev

@dorsev dorsev force-pushed the enhance-support-for-option-either branch from f7cf6b4 to b6d79cd Compare July 13, 2019 10:14
@dorsev
Copy link
Contributor Author

dorsev commented Jul 13, 2019

Thank you very much for the help @iravid !!! much appreciated!
Fixed the commits accordingly.

A thought: Should I add a custom "message" for every "NoSuchElementException" like in None.get or leave it empty?

@iravid
Copy link
Member

iravid commented Jul 13, 2019

Yeah, a custom message would be a nice touch 👍

@dorsev dorsev force-pushed the enhance-support-for-option-either branch from b6d79cd to fd47eef Compare July 13, 2019 14:34
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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]].

@iravid
Copy link
Member

iravid commented Jul 14, 2019

Two small comments and this is good to go.

@dorsev dorsev force-pushed the enhance-support-for-option-either branch from dfb2dc4 to b3b5acd Compare July 14, 2019 18:42
Copy link
Member

@iravid iravid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work!

@iravid iravid merged commit 0f437d1 into zio:master Jul 15, 2019
@jdegoes
Copy link
Member

jdegoes commented Jul 15, 2019

@dorsev Thank you for your contribution!

ghostdogpr pushed a commit to ghostdogpr/scalaz-zio that referenced this pull request Jul 26, 2019
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhance support for Option and Either

4 participants