Skip to content

Instantly share code, notes, and snippets.

@loicdescotte
Last active July 10, 2018 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loicdescotte/a3cab52dcf3e62483105bafb3e3d910e to your computer and use it in GitHub Desktop.
Save loicdescotte/a3cab52dcf3e62483105bafb3e3d910e to your computer and use it in GitHub Desktop.
Tagless final with MonadError
import cats._
import cats.implicits._
def loadResult[F[_]](implicit F: MonadError[F, Throwable]): F[String] = {
val resultEitherF: F[Either[Throwable, String]] = ???
val resultF: F[String] = resultEitherF.flatMap(F.fromEither)
resultF.recoverWith{
case error => F.pure(error.getMessage)
}
}
type Throwing[F[_]] = cats.MonadError[F, Throwable]
def loadResult[F[_] : Throwing]: F[String] = {
val resultEitherF: F[Either[Throwable, String]] = ???
val resultF: F[String] = resultEitherF.flatMap(MonadError[F, Throwable].fromEither)
resultF.recoverWith{
case error => MonadError[F, Throwable].pure(error.getMessage)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment