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

Skip to content

Conversation

@paulpdaniels
Copy link
Contributor

Adds a new fromOptionError operator which has better composability with the existing some/optional/asSomeError combinators.

@adamgfraser
Copy link
Contributor

I think this is definitely interesting. Have you explored using Option[Nothing] instead of Unit more broadly for the error type? The two types are isomorphic but agree this is more composable so wonder if this is the more practical signature, though it is pretty late for API changes.

@iravid
Copy link
Member

iravid commented Jun 15, 2020

I find this vastly more useful than the current fromOption, FWIW.

@adamgfraser
Copy link
Contributor

Yes. I wonder if we just move everything over.

@paulpdaniels
Copy link
Contributor Author

Yes. I wonder if we just move everything over.

I'm ok with moving everything over to this. I use this extension locally in most of my projects because I wanted a drop in replacement for OptionT. I don't think I have any current instances of fromOption because they always require mapError or asError directly after them to make them work with other operators.

Also I'd prefer to move, because the naming of this operator is somewhat confusing, it would be better for it to just be the defacto.

@iravid
Copy link
Member

iravid commented Jun 16, 2020 via email

val zoption: IO[Option[Nothing], Int] = ZIO.fromOption(Some(2))
```

The error type of the resulting effect is `Unit`, because the `None` case of `Option` provides no information on why the value is not there. You can change the `Unit` into a more specific error type using `ZIO#mapError`:

Choose a reason for hiding this comment

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

Does this paragraph need to be updated in lieu of the error channel now being defined as Option[Nothing] instead of Unit?

@jdegoes
Copy link
Member

jdegoes commented Jun 16, 2020

Agree to use Option[Nothing] instead of Unit for fromOption and related cases. 👍

You can also readily compose it with other operators while preserving the optional nature of the result (similar to an `OptionT`)

```scala mdoc:silent
val maybeId: IO[Option[Nothing], String] = ZIO.fromOption(Some("abc123"))
Copy link
Member

Choose a reason for hiding this comment

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

Really nice example!

@jdegoes
Copy link
Member

jdegoes commented Jun 16, 2020

@paulpdaniels Made a few comments but looks really good! Let me know when it's ready & tests are passing. Thank you for your work on this!

@paulpdaniels
Copy link
Contributor Author

@jdegoes comments addressed!

1 similar comment
@paulpdaniels
Copy link
Contributor Author

@jdegoes comments addressed!

Copy link
Contributor

@adamgfraser adamgfraser left a comment

Choose a reason for hiding this comment

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

Looks great!

@adamgfraser adamgfraser merged commit 556af9d into zio:master Jun 19, 2020
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.

5 participants