我找type macros
了斯卡拉。但是当我想从示例中创建对象时,我得到了错误:
Example.scala:7: `=', `>:', or `<:' expected
type Test(url: String) = macro impl
Example.scala:12: illegal start of simple expression
val clazz = ClassDef(..., Template(..., generateCode()))
代码:
//Example.sbt
object Example {
type Test(url: String) = macro impl
def impl(c:Context)(url: c.Expr[String]):c.Tree = {
import c.universe._
val name = c.freshName(c.enclosingImpl.name).toTypeName
val clazz = ClassDef(..., Template(..., generateCode()))
c.introduceTopLevel(c.enclosingPackage.pid.toString, clazz)
val classRef = Select(c.enclosingPackage.pid, name)
Apply(classRef, List(Literal(Constant(c.eval(url)))))
}
}
斯卡拉版本:2.10.2
来自:类型宏