Cats: EitherT.fromMonadError

Created on 26 Nov 2019  路  4Comments  路  Source: typelevel/cats

I think #1644 is related.

Basicly something like:

import cats.implicits._
import cats.data.EitherT
import cats.MonadError

def fromMonadError[F[_], A, B](me: F[B])(implicit ev: MonadError[F, A]) = {
  EitherT(me.map(Either.right[A, B]).recover{case ex => Either.left[A, B](ex)})
}

Or if we have this functionality already maybe it would worth a mention in the EitherT/MonadError documentation.

If others think this is useful, I try to PR it :)

Most helpful comment

It seems this already exists and is called attemptT.

All 4 comments

I think this is neat, though I believe it could be more simply implemented with attempt. Something like this:

def fromMonadError[F[_], E, A](fa: F[A])(implicit F: MonadError[F, E]): EitherT[F, E, A] =
  EitherT(F.attempt(fa))

It's kind of like a mechanism for lifting into a redundant signal channel. I do stuff like this semi-frequently; I'm curious if others do as well.

It comes up surprisingly often for me as well.

It seems this already exists and is called attemptT.

Nice! So can we add this to the EitherT docs?

Was this page helpful?
0 / 5 - 0 ratings