onSuccess 和 onFailure 在 def 中的最后一条语句时没有 return 任何东西
onSuccess and onFailure doesn't return anything when they are last statement in def
我正在尝试处理期货集合,但在 return 根据未来状态从 def 计算结果时遇到了问题。下面是我的代码:
final case class StagesToRun(stages : Set[StageRun])
private def processNextStagesAndAccumulateResults(stagesToRun: StagesToRun): \/[Exception, Success] = {
val stageProcessingExceptions = mutable.Set[Exception]()
//processor.process(stagesToRun) => returns a Set[Future[\/[Exception, Success]]] and I am converting it to Future[Set[\/[Exception, Success]]] in below expression
val processResults = Future.sequence(processor.process(stagesToRun))
processResults.onSuccess {
case result => {
result.map { res =>
res.fold(
l => stageProcessingExceptions += l,
r => r
)
}
if (stageProcessingExceptions.isEmpty) Success.right
else new Exception("Got exception while processing one of the stage").left
}
}
processResults.onFailure {
case ex => new Exception(ex.getMessage)).left
}
}
现在,按照 Scala 约定,我函数的最后一条语句成为我函数的 return 语句。在这个函数中,它应该基本上是 if (stageProcessingExceptions.isEmpty) Success
的输出及其对应的 else
或 onFailure
的结果,即 new Exception(ex.getMessage))
。然而,编译器一直告诉我 return 类型是单元而不是预期的析取。有人可以帮我吗?谢谢
当你说函数的最后一条语句变成 return 语句时,你是绝对正确的。但是,如果你看到 onSuccess
和 onFailure
的方法定义,它们都是 return Unit
作为 return 类型。
来自 scala 文档,onSuccess 的签名是
def onSuccess[U](pf: PartialFunction[T, U])(implicit executor: ExecutionContext): Unit = onComplete {
case Success(v) =>
pf.applyOrElse[T, Any](v, Predef.identity[T]) // Exploiting the cached function to avoid MatchError
case _ =>
}
在类似的线上 onFailure returns 单元。
def onFailure[U](@deprecatedName('callback) pf: PartialFunction[Throwable, U])(implicit executor: ExecutionContext): Unit = onComplete {
case Failure(t) =>
pf.applyOrElse[Throwable, Any](t, Predef.identity[Throwable]) // Exploiting the cached function to avoid MatchError
case _ =>
}
在您的情况下,您可以对未来而不是 onComplete 应用地图功能。这将帮助您传播所需的类型。
另外,如果你想处理你的未来失败的情况,你可以为你的未来添加一个恢复块作为
.recover {
case _ =>
//whatever type you want to propogate ex: Left()
}
onSuccess
和 onFailure
return Unit
都是为实现副作用而设计的。如果要 return 修改 Future
,请使用 transform
。其中一个版本采用两个函数:第一个函数处理成功结果,第二个函数处理异常。
processResults.transform(
{ result =>
// process result and return new result
// throw error on failure
result
},
{ ex =>
// Process exception and return new exception
ex
}
)
如果您在任一函数中抛出异常,您将收到错误 Future
。
还有另一个版本的 transform
,它带有一个函数 Try => Try
,它允许您将错误 [=14=] 变成成功 Future
,这是不可能的以上版本。
我正在尝试处理期货集合,但在 return 根据未来状态从 def 计算结果时遇到了问题。下面是我的代码:
final case class StagesToRun(stages : Set[StageRun])
private def processNextStagesAndAccumulateResults(stagesToRun: StagesToRun): \/[Exception, Success] = {
val stageProcessingExceptions = mutable.Set[Exception]()
//processor.process(stagesToRun) => returns a Set[Future[\/[Exception, Success]]] and I am converting it to Future[Set[\/[Exception, Success]]] in below expression
val processResults = Future.sequence(processor.process(stagesToRun))
processResults.onSuccess {
case result => {
result.map { res =>
res.fold(
l => stageProcessingExceptions += l,
r => r
)
}
if (stageProcessingExceptions.isEmpty) Success.right
else new Exception("Got exception while processing one of the stage").left
}
}
processResults.onFailure {
case ex => new Exception(ex.getMessage)).left
}
}
现在,按照 Scala 约定,我函数的最后一条语句成为我函数的 return 语句。在这个函数中,它应该基本上是 if (stageProcessingExceptions.isEmpty) Success
的输出及其对应的 else
或 onFailure
的结果,即 new Exception(ex.getMessage))
。然而,编译器一直告诉我 return 类型是单元而不是预期的析取。有人可以帮我吗?谢谢
当你说函数的最后一条语句变成 return 语句时,你是绝对正确的。但是,如果你看到 onSuccess
和 onFailure
的方法定义,它们都是 return Unit
作为 return 类型。
来自 scala 文档,onSuccess 的签名是
def onSuccess[U](pf: PartialFunction[T, U])(implicit executor: ExecutionContext): Unit = onComplete {
case Success(v) =>
pf.applyOrElse[T, Any](v, Predef.identity[T]) // Exploiting the cached function to avoid MatchError
case _ =>
}
在类似的线上 onFailure returns 单元。
def onFailure[U](@deprecatedName('callback) pf: PartialFunction[Throwable, U])(implicit executor: ExecutionContext): Unit = onComplete {
case Failure(t) =>
pf.applyOrElse[Throwable, Any](t, Predef.identity[Throwable]) // Exploiting the cached function to avoid MatchError
case _ =>
}
在您的情况下,您可以对未来而不是 onComplete 应用地图功能。这将帮助您传播所需的类型。 另外,如果你想处理你的未来失败的情况,你可以为你的未来添加一个恢复块作为
.recover {
case _ =>
//whatever type you want to propogate ex: Left()
}
onSuccess
和 onFailure
return Unit
都是为实现副作用而设计的。如果要 return 修改 Future
,请使用 transform
。其中一个版本采用两个函数:第一个函数处理成功结果,第二个函数处理异常。
processResults.transform(
{ result =>
// process result and return new result
// throw error on failure
result
},
{ ex =>
// Process exception and return new exception
ex
}
)
如果您在任一函数中抛出异常,您将收到错误 Future
。
还有另一个版本的 transform
,它带有一个函数 Try => Try
,它允许您将错误 [=14=] 变成成功 Future
,这是不可能的以上版本。