-
-
Notifications
You must be signed in to change notification settings - Fork 64
Description
Are cats.mtl.Handle[F[_], E] and cats.ApplicativeError[F[_], E] the same abstraction?
I can't see a meaningful difference between them. Handle includes code to derive itself from ApplicativeError. The reverse derivation also seems possible. So they appear isomorphic.
Which suggests the machinery for submarine error propagation could equally be supported for ApplicativeError, potentially reaching the wider audience of Cats users.
It also seems confusing, to me at least, to have two isomorphic type-classes in the same ecosystem.
There is also cats.MonadError, a subclass of ApplicativeError which is often more useful in practice, because it permits sequencing operations with flatMap - a common requirement of application code.
MonadError[F, E] suffices to express fallible-but-othewise-pure computations and neatly abstracts across concrete effects Either, and the more powerful cats.effect.IO (with submarines to permit non-Throwable error types). So the ability to derive Handle+Monad=>MonadError seems valuable.
given [F[_]: Monad as m, E](using h: Handle[F, E]): MonadError[F, E] = new MonadError:
def pure[A](x: A): F[A] = h.applicative.pure(x)
def handleErrorWith[A](fa: F[A])(f: E => F[A]): F[A] = h.handleWith(fa)(f)
def raiseError[A](e: E): F[A] = h.raise(e)
def flatMap[A, B](fa: F[A])(f: A => F[B]): F[B] = m.flatMap(fa)(f)
def tailRecM[A, B](a: A)(f: A => F[Either[A, B]]): F[B] = m.tailRecM(a)(f)