Scala 用函数替换异步匿名类

Scala 用函数替换异步匿名类,scala,shapeless,Scala,Shapeless,我认为这可能与无形的图书馆 我使用shapeless将匿名类转换为闭包。这需要使用FnHListerAux特性中的hlisted 我所要做的就是去掉传入的伪函数,并在该函数周围返回一个闭包,该闭包具有与F相同的类型签名。如果没有异步执行的匿名类,这将很容易。有没有办法绕过这个问题 def async[F, A <: HList, R]( shell: Shell, success: F, failure: FunctionTypes.Failure, dummy: F)(i

我认为这可能与无形的图书馆

我使用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
def async[F,A R],
u:FnUnHListerAux[A=>R,F]):F=
{(args:A)=>
require(shell!=null,“shell不能为null”)
require(shell.getDisplay()!=null,“shell必须有一个显示”)
val display=shell.getDisplay()
display.asyncExec(新的Runnable(){
def run():单位={
试一试{
success.hlisted(args)
}抓住{
案例e:可丢弃=>
故障(e)
}
}
})
dummy.hlisted(args)
}.未列入名单

我将从简化一点开始。假设我有一个函数
f
。我事先不知道它是arity,我也不关心它的回报。我想用一些函数来包装它,并得到一个具有相同参数类型的函数。我也不关心这个结果函数返回什么,所以我最好让它返回
Unit

您可以编写一组(好吧,22个)函数,如下所示:

def wrap[A](f: A => Unit): A => Unit = ???
def wrap[A, B](f: (A, B) => Unit): (A, B) => Unit = ???
def wrap[A, B, C](f: (A, B, C) => Unit): (A, B, C) => Unit = ???
def async[F, A <: HList](
  shell: Shell, success: F, failure: FunctionTypes.Failure
)(
  implicit h: FnHListerAux[F, A => Unit], u: FnUnHListerAux[A => Unit, 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)
      }
    }
  })
}.unhlisted
但你不想

Shapeless绝对可以帮助您更全面地解决此问题:

def wrap[F, A <: HList](f: F)(
  implicit h: FnHListerAux[F, A => Unit], u: FnUnHListerAux[A => Unit, F]
): F = { (args: A) =>
  println("Before!"); f.hlisted(args); println("After!")
}.unhlisted

不需要
dummy

如果你需要这样毫无意义的东西,你的算法肯定有一些严重的问题。试着解释一下你需要它做什么。当然,那是个好主意。我对斯卡拉很陌生。我仍然很好奇,是否可以替换函数体。然而,对于我的实际问题,解决方案很可能是在不成形的库中,这远远超出了我的知识水平,我仍在努力理解它的本质。
def async[F, A <: HList](
  shell: Shell, success: F, failure: FunctionTypes.Failure
)(
  implicit h: FnHListerAux[F, A => Unit], u: FnUnHListerAux[A => Unit, 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)
      }
    }
  })
}.unhlisted