Some time ago I've started using Cats and found OptionT
very useful to work with Future[Option[T]]
in most cases. But I faced with one drawback, to use AplicativeError
I need to define type alias type FutureOption[T] = OptionT[Future, X]
to matching F[_]
required by AplicativeError
and explicitly specify the type of my expression as FutureOption[T]
.
type FutureOption[T] = OptionT[Future, T] // definition to match F[_] kind
val x = OptionT.liftF(Future.failed(new Exception("error"))) : FutureOption[String] // need to specify type explicitly
x.recover {
case NonFatal(e) => "fixed"
}
If I remove type definition and explicit type specification of my expression the recover
will not be available because OptionT[Future, T]
don't match F[_]
, so it can't be converted implicitly to AplicativeErrorOps
.
Unfortunately, the example below won't work because there is no recover
method.
val x = OptionT.liftF(Future.failed(new Exception("error")))
x.recover {
case NonFatal(e) => "fixed"
}
Is there any way to avoid such kind of boilerplate code? At least I want to avoid specifying expression types as FutureOption[T]
explicitly.