Skip to content
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

Add getOrRaiseF #323

Merged
merged 1 commit into from
Mar 19, 2022
Merged

Conversation

geirolz
Copy link
Contributor

@geirolz geirolz commented Mar 18, 2022

Hi, dealing with nested effects is always complex and sometimes verbose, I found helpful these two methods in my codebase and I'd like to propose them here.

getOrRaiseF and getOrRaiseMsgF given F[G[A]] where F is a MonadError and G a foldable like Option and Either we can either get the nested value or raise an error in F if the value is missing ( since either is right biased).

Example

val fga: Try[Option[Int]] = Success(None)
val result: Try[Int] = fga.getOrRaiseMsgF("BOOM!")// = Failure(RuntimeException("BOOM!")

What do you think? (There are alternatives already in place? )

Copy link
Member

@danicheg danicheg left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!
If one speak about alternatives, I could advise something like flatMap + ApplicativeError.fromEither / flatMap + ApplicativeError.fromOption. But it's not so handy indeed.

shared/src/main/scala/mouse/feither.scala Outdated Show resolved Hide resolved
shared/src/main/scala/mouse/foption.scala Outdated Show resolved Hide resolved
shared/src/main/scala/mouse/feither.scala Outdated Show resolved Hide resolved
shared/src/main/scala/mouse/foption.scala Outdated Show resolved Hide resolved
@benhutchison
Copy link
Member

Overall, I'm feeling 👍 about the PR.

In terms of code, the PR is good. My only doubt, which as a maintainer I must consider, is "is the user base for this feature larger than the original contributor?"

In this case, given we already provide extensive support for working with F[Either] and F[Option] I think there are likely mouse users who could use these operations.

Re the getOrRaiseMsgF variants: I think I added toTryMsg years ago, and it does align with my experience that often, an error value is simply a string; it's intended to be read by a human, not pattern matched on. I'm OK with adding them, to save the user from having to wrap up a String into a Throwable.

@benhutchison benhutchison merged commit 7229d85 into typelevel:main Mar 19, 2022
@geirolz geirolz deleted the add-get-or-raise-methods branch March 19, 2022 22:51
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.

None yet

3 participants