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 getOrRaise for OptionT, EitherT and IorT #4212

Merged

Conversation

geirolz
Copy link

@geirolz geirolz commented May 24, 2022

I've recently opened and merged a PR for mouse adding getOrRaise method to F[Option[A]] and F[Either[A, B]].
typelevel/mouse#323

Since I found this method useful I'd like to introduce it to OptionT, EitherT and IorT.

What do you think ?


P.S. For mouse, I've added getOrRaiseMsg as well which accepts a String and lifts it to RuntimeException.
Honestly, I've always missed this feature in cats since in 99% of the cases I've seen this:

IO.raiseError(new RuntimeException("ERROR"))

It would be great to have either

  • raiseErrorMsg which accepts a string
  • An overload of raiseError which accepts a string
  • A string interpolator to lift a string to an Exception like val ex: RuntimeException = ex"ERROR" in order to keep all other API unchanged

@armanbilge
Copy link
Member

armanbilge commented May 24, 2022

Thanks for the PR! Is this syntax the same ideas as orRaise?
https://typelevel.org/cats/api/cats/syntax/ApplicativeErrorOps.html#orRaise(other:=%3EE)(implicitF:cats.ApplicativeError[F,E]):F[A]

def orRaise(other: => E)(implicit F: ApplicativeError[F, E]): F[A]
scala> import cats._, implicits._

scala> val fa: Either[String, Int] = Left("wrong")

scala> fa.orRaise("wronger")
res1: Either[String,Int] = Left(wronger)

scala> val fb: Either[String, Int] = Right(42)

scala> fb.orRaise("wrongest")
res2: Either[String,Int] = Right(42)

Edit: right, I see, you need the get to unpack it from the Option/Either/etc. It reminds me of this blogpost.
https://typelevel.org/blog/2018/04/13/rethinking-monaderror.html

@armanbilge armanbilge added this to the 2.8.0 milestone May 24, 2022
@geirolz geirolz force-pushed the add-get-or-raise-for-opt-either-ior-mt branch from 0410d24 to bc395c6 Compare May 24, 2022 19:57
@geirolz
Copy link
Author

geirolz commented May 24, 2022

Edit: right, I see, you need the get to unpack it from the Option/Either/etc. It reminds me of this blogpost. https://typelevel.org/blog/2018/04/13/rethinking-monaderror.html

@armanbilge MonadBlunder looks a very idiomatic way to express/handle errors, or at least simplify complex cases where you have to deal with "always succeed" monad. At the moment unpacking and switching between Option, Either, Ior and their MT is still too complex and verbose IMHO.
The post is dated 2018 are you still thinking about it or are you working on another solution?

@armanbilge
Copy link
Member

I'm not currently working on anything, but it's an interesting idea and your PR reminded me of this problem :)

I'm not sure if such a new idea could land in Cats due to the burden of guaranteeing long-term compatibility. However, I wonder if a 3rd party library can explore this idea by creating a Parallel-like typeclass that relates a MonadError datatype (e.g. IO) to its error-free Monad counterpart (e.g., some IO newtype that only implements Monad, but that seems rather strange 🤔 ). For Either or Option it would simply be Id I believe.

@armanbilge
Copy link
Member

Ha, that seems to be this: https://github.com/LukaJCB/cats-uio

Maybe that repo just needs to be refreshed by an interested person :)

@geirolz
Copy link
Author

geirolz commented May 26, 2022

Ha, that seems to be this: https://github.com/LukaJCB/cats-uio

Maybe that repo just needs to be refreshed by an interested person :)

I'll try to have a look, even just for Option and Either would be great! Especially when you have to deal with nested monads 😊 I see @LukaJCB already reached a great point there!

MonadBlunder aside, @armanbilge @satorg what do think about getOrRaise ?

Copy link
Contributor

@satorg satorg left a comment

Choose a reason for hiding this comment

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

lgtm, thanks!

@armanbilge armanbilge merged commit a03fc6b into typelevel:main May 28, 2022
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.

3 participants