问题描述
我想实现一些特征 proxy
的 A
(例如将方法调用委托给一些 rpc 调用),就像这样
def clientProxy[A](using Type[A],Quotes): Expr[A] = {
import quotes.reflect._
val defTrees: List[Tree] = TypeRepr.of[A].typeSymbol.memberFields.collect {
case mf if mf.isDefDef =>
???
}
val exprs = Expr.ofList(defTrees.map(_.asExpr))
'{
new A {
$exprs
}
}
}
但编译器抱怨
A is not a class type
解决方法
如果A
是一类,你可以尝试更换
'{
new A {
$exprs
}
}
与
Apply(
Select.unique(New(TypeTree.of[A]),"<init>"),defTrees.map(_.asExpr.asTerm)
).asExprOf[A]
(Scala 3.0.0-RC1-bin-20210106-e39b79e-NIGHTLY)
How to access parameter list of case class in a dotty macro
现在,因为A
是一个特点,我想你应该定义一个类实现这个特点,并尝试类似的事情,这个类。