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]
}
}
I would like to modify the macro above and add type parameter [A]. I tried the following but it does not compile
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]
}
}