【发布时间】:2012-07-18 00:24:54
【问题描述】:
我认为无形库可以做到这一点。
我正在使用 shapeless 将匿名类转换为闭包。这需要使用来自 FnHListerAux 特征的 hlisted。
我想要做的就是摆脱传入的虚拟函数,并围绕该函数返回一个闭包,该闭包具有与F 相同的类型签名。如果没有异步执行的匿名类,这将很容易。有没有办法解决这个问题?
def async[F, A <: HList, R](
shell: Shell,
success: F,
failure: FunctionTypes.Failure,
dummy: F)(implicit h: FnHListerAux[F, A => R],
u: FnUnHListerAux[A => R, F]): F =
{ (args: A) =>
require(shell != null, "Shell cannot be null")
require(shell.getDisplay() != null, "The shell must have a display")
val display = shell.getDisplay()
display.asyncExec(new Runnable() {
def run(): Unit = {
try {
success.hlisted(args)
} catch {
case e: Throwable =>
failure(e)
}
}
})
dummy.hlisted(args)
}.unhlisted
【问题讨论】:
-
如果你需要这样毫无意义的东西,你的算法肯定有一些严重的问题。试着解释一下你需要什么。
-
当然,那是个好主意。我对 Scala 很陌生。我仍然很好奇你是否真的可以替换函数的主体。但是,对于我的实际问题,解决方案很可能在于无形库,这远远超出了我的知识水平,我仍在努力了解它的本质。