我想实现某个特征A的proxy (例如,将方法调用委托给某个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发布于 2021-01-07 23:56:42
如果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是一个特征,我想你应该定义一个实现这个特征的类,并尝试对这个类做类似的事情。
https://stackoverflow.com/questions/65561193
复制相似问题