相关疑难解决方法(0)

在Scala宏注释上键入参数

我想在Scala中,我的宏注释会采取另一种类型的参数使用宏注解.然后,它会使用Scala的反射来看看式传递,并添加一些方法如appropriate.Eg.

trait MyTrait {
  def x: Int
  def y: Float
}

@MyAnnotation class MyClass //<-- somehow, this annotation should reference MyTrait

class MyAnnotation(val target: Any) extends StaticAnnotation {
  def macroTransform(annottees: Any*) = macro MyAnnotationImpl.impl
}
object MyAnnotationImpl {
  def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
    // if I can get a handle on the type MyTrait in here
    // then I can call .members on it, etc.
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

基本上,与使用Scala宏中的Scala反射相同,除了使用宏注释.但是,当我尝试使用TypeTag模拟我的宏注释时

class MyAnnotation[T](val target: Any) extends StaticAnnotation { …
Run Code Online (Sandbox Code Playgroud)

reflection scala-macros

4
推荐指数
1
解决办法
1382
查看次数

标签 统计

reflection ×1

scala-macros ×1