gpt4 book ai didi

scala - 对scala使用局部函数来捕获 block

转载 作者:行者123 更新时间:2023-12-04 13:20:29 25 4
gpt4 key购买 nike

我有一个catch块,最后我重复了很多次,发现这个SO问题确认了我可以将部分函数用作catch块(Scala 2.9的try…catch概括化的用例是什么?1) 。

目前,我的代码如下所示:

var task = newTask("update product", "update for customer " + customerId.toString)
try {
val vcdRouter = actorSystem.actorFor("user/supervisor/router-10.10.10.10:443")

val vdcId = new UUID("92ddba5e-8101-4580-b9a5-e3ee6ea5718f")
val vdcGet = sendExpect[AdminVdcType](vcdRouter, GetVdc(vdcId))
val vdcPut = VdcPutConfig(vdcGet, c)
val vdcPutTask = sendExpect[TaskType](vcdRouter, UpdateVdc(vdcId, vdcPut))

task = task.copy(Progress = 100, status = SuccessType)

} catch {
case failure: NoResponseBodyException =>
logger.debug("*** In putCustomerProduct, got a Left(VcdGatewayException)")
task = task.copy(Progress = 100, status = Error, Error = Option(exceptionToError(failure, BadGateway)))

case failure: VcdGatewayException ⇒
logger.debug("*** In putCustomerProduct, got a Left(VcdGatewayException)")
task = task.copy(Progress = 100, status = Error, Error = Option(exceptionToError(failure, GatewayTimeout)))

case failure: Exception ⇒
logger.debug("*** In putCustomerProduct, got a Left(Exception)")
task = task.copy(Progress = 100, status = Error, Error = Option(exceptionToError(failure)))

}

由于我有在catch块内更改的任务var,是否有一种很好的方法可以从保存catch块的部分函数中对其进行访问?该任务是一个变量,因为它在进入系统后会设置一些初始化数据,例如创建的时间戳。我可以解决此问题,但是无论如何我都希望对原始问题的答案感兴趣。

最佳答案

我假设您要使用几个具有不同var task的不同函数。

您可以创建一个同时使用task和任务设置程序作为参数的函数,该函数返回可以用作捕获处理程序的PartialFunction

def handler(task: Task, setTask: Task => Any): PartialFunction[Throwable, Any] = {
case failure: NoResponseBodyException =>
logger.debug("*** In putCustomerProduct, got a Left(NoResponseBodyException)")
setTask(task.copy(Progress = 100, status = Error, Error = Option(exceptionToError(failure, BadGateway))))

case failure: VcdGatewayException =>
logger.debug("*** In putCustomerProduct, got a Left(VcdGatewayException)")
setTask(task.copy(Progress = 100, status = Error, Error = Option(exceptionToError(failure, GatewayTimeout))))

case failure: Exception =>
logger.debug("*** In putCustomerProduct, got a Left(Exception)")
setTask(task.copy(Progress = 100, status = Error, Error = Option(exceptionToError(failure))))
}

// somewhere else in your code...
var task = newTask("update product", "update for customer " + customerId.toString)
try {
val vcdRouter = actorSystem.actorFor("user/supervisor/router-10.10.10.10:443")

val vdcId = new UUID("92ddba5e-8101-4580-b9a5-e3ee6ea5718f")
val vdcGet = sendExpect[AdminVdcType](vcdRouter, GetVdc(vdcId))
val vdcPut = VdcPutConfig(vdcGet, c)
val vdcPutTask = sendExpect[TaskType](vcdRouter, UpdateVdc(vdcId, vdcPut))

task = task.copy(Progress = 100, status = SuccessType)
} catch handler(task, task = _)

我也同意 user3001,您应该尝试减少catch处理程序中的重复项。

关于scala - 对scala使用局部函数来捕获 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10452489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com