【发布时间】:2017-01-21 18:05:56
【问题描述】:
package scalaworld.macros
import scala.meta._
class Argument(arg: Int) extends scala.annotation.StaticAnnotation {
inline def apply(defn: Any): Any = meta {
println(this.structure)
val arg = this match {
// The argument needs to be a literal like `1` or a string like `"foobar"`.
// You can't pass in a variable name.
case q"new $_(${Lit(arg: Int)})" => arg
// Example if you have more than one argument.
case q"new $_(${Lit(arg: Int)}, ${Lit(foo: String)})" => arg
case _ => ??? // default value
}
println(s"Arg is $arg")
defn.asInstanceOf[Stat]
}
}
我想修改上面的宏并添加类型参数[A]。 我尝试了以下但它没有编译
package scalaworld.macros
import scala.meta._
class Argument2[A](arg: A) extends scala.annotation.StaticAnnotation {
inline def apply(defn: Any): Any = meta {
println(this.structure)
val arg = this match {
case q"new $_(${Lit(arg: A)})" => arg
case q"new $_(${Lit(arg: A)}, ${Lit(foo: String)})" => arg
case _ => ???
}
println(s"Arg is $arg")
defn.asInstanceOf[Stat]
}
}
【问题讨论】:
-
我只是想做一些类似于这里描述的事情github.com/scalameta/sips/blob/…
标签: scala scala-macros scalameta