Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 使用Future.flow-CPS延续传递样式时如何进行错误处理_Scala_Akka - Fatal编程技术网

Scala 使用Future.flow-CPS延续传递样式时如何进行错误处理

Scala 使用Future.flow-CPS延续传递样式时如何进行错误处理,scala,akka,Scala,Akka,我试图在下面的函数中实现错误处理。我的问题是,每当我知道如何使用MyValue作为参数时,我都需要展开Future[Exception,MyValue]]并测试“Right” 策略是让Future.flow在f1到f4中的任何一个失败时失败 一种解决方案是将[Exception,MyValue]作为参数传递给funcReturnFutureEitherX(),类似于funcReturnFutureEither3(f2()),但这只是向下传播错误处理,而不是在主程序MyFlowFunc()中处理它

我试图在下面的函数中实现错误处理。我的问题是,每当我知道如何使用MyValue作为参数时,我都需要展开Future[Exception,MyValue]]并测试“Right”

策略是让Future.flow在f1到f4中的任何一个失败时失败

一种解决方案是将[Exception,MyValue]作为参数传递给funcReturnFutureEitherX(),类似于funcReturnFutureEither3(f2()),但这只是向下传播错误处理,而不是在主程序MyFlowFunc()中处理它

如何在下面的函数中实现错误处理并使其运行无阻塞

def MyFlowFunc():Future[(MyValue,MyValue,MyValue,MyValue)] =
    val v = Future.flow {
        // fail flow if below functions fails with a Either.Left() 
        val f1 = funcReturnFutureEither1()   
        val f2 = funcReturnFutureEither2(f1())
        val f3 = funcReturnFutureEither3(f2())
        val f4 = funcReturnFutureEither4(f1())
        val res = (f1(),f2(),f3(),f4())
    }
}
def funcReturnEitherX(val:MyValue):Future[Either[Exception,MyValue]]

如果您使用
scalaz
,以下内容可能会有意义。如果不是,那么读一下它会很有趣:

任一[L,R]
同构于
验证[E,A]
,但后者通常用作
应用函子。由于
Future
也是一个应用函子,因此它们的组合类型
Future[Validation[E,A]]]
也是一个应用函子

然后您可以使用
@
操作符组合参数并将其传递到无上下文函数中。实际上,您可能需要为将来的
编写
应用程序
类型类


更新:参见示例代码。

好的,我自己找到了答案。不要使用或选项进行任何错误处理,只需使用fallbackTo(Future(None))。这将在上面的代码中转换为v.fallbackTo(Future(None))。顺便说一句,这听起来很有趣,我可能会尝试在实践中实现它,并返回一个示例。