【发布时间】:2018-02-24 04:01:26
【问题描述】:
前段时间我开始使用 Cats,发现 OptionT 在大多数情况下与 Future[Option[T]] 一起工作非常有用。但是我面临一个缺点,要使用AplicativeError,我需要定义类型别名type FutureOption[T] = OptionT[Future, X] 以匹配AplicativeError 所需的F[_],并将我的表达式类型明确指定为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"
}
如果我删除表达式的类型定义和显式类型规范,recover 将不可用,因为 OptionT[Future, T] 不匹配 F[_],因此它无法隐式转换为 AplicativeErrorOps。
很遗憾,下面的示例不起作用,因为没有 recover 方法。
val x = OptionT.liftF(Future.failed(new Exception("error")))
x.recover {
case NonFatal(e) => "fixed"
}
有没有办法避免这种样板代码?至少我想避免将表达式类型明确指定为FutureOption[T]。
【问题讨论】:
标签: scala scala-cats