由类的方法返回类型参数化的方法



是否可以获取未初始化的case class方法的返回类型?我想创建一个像这样工作的包装器:

abstract class Node[L, R]{
    def call(args: L): R
}
case class Kls(arg1: Int, arg2: Int) {
    def apply() = arg1 + arg2
}
object Node {
def apply[L <: { def apply(): R }, R](implicit lgen: LabelledGeneric[L]): Node[lgen.Repr, R] = {
  new Node[lgen.Repr, R] {
    def call(args: lgen.Repr): R = {
      lgen.from(args).apply()
    }
  }
}
}
val n = Node[Kls] // does not compile - missing type param R
n.call(arg1 :: arg2 :: HNil) //should have the right return type

或者,是否有FTo标签产品?我需要什么样的宏观功夫来创造一个?

也许我误解了,但我认为你根本不需要L

case class Kls(arg1: Int, arg2: Int) {
  def apply() = arg1 + arg2
}
abstract class Node[L, R]{
  def call(args: L): R
}
import shapeless._
object Node {
  def apply[R](implicit gen: LabelledGeneric[R]): Node[gen.Repr, R] =
    new Node[gen.Repr, R] {
      def call(args: gen.Repr): R = gen.from(args)
    }
}

然后:

import shapeless.syntax.singleton._
val n = Node[Kls]
val result = n.call('arg1 ->> 1 :: 'arg2 ->> 2 :: HNil)

其中result将被静态地键入为Kls。这就是你要找的吗?

我用一个简单的宏解决了这个问题:

trait CallApply[C] {
  type Ret
  def apply(c: C): Ret
}
object CallApply {
  type Aux[C, R] = CallApply[C] { type Ret = R }
  implicit def materialize[C, R]: Aux[C, R] = macro CallApplyImpl.materialize[C]
}
object CallApplyImpl {
  import scala.reflect.macros.whitebox
  def materialize[C: c.WeakTypeTag](c: whitebox.Context): c.Tree = {
    import c.universe._
    val C = weakTypeOf[C]
    val assignM = C.decls.collect {
      case sym: MethodSymbol if sym.name == TermName("apply") => sym
    }
    if (assignM.headOption.isEmpty) c.abort(c.enclosingPosition, "case class must define an apply() method")
    val R = assignM.head.returnType
    q"""new _root_.fwb.api.CallApply[$C] { type Ret = $R; def apply(c: $C) : $R = c.apply() }"""
  }
}

用法:

  object Node {
    def call[L](implicit lgen: LabelledGeneric[L], ev: CallApply[L]): Node[lgen.Repr, ev.Ret] = {
      new Node[lgen.Repr, ev.Ret] {
        def call(args: lgen.Repr): ev.Ret = {
          ev(lgen.from(args))
        }
      }
    }
  }

val n = Node[Kls]的工作与预期的一样。然而,如果可能的话,看到一个没有元编程的解决方案会很好。

最新更新