我正在尝试实现一个隐式的物化器,如下所述:
http://docs.scala-lang.org/overviews/macros/implicits.html
我决定创建一个宏,使用quasiquotes将case类从String转换为String,以进行原型设计.例如:
case class User(id: String,name: String) val foo = User("testid","foo")
将foo转换为文本应该导致“testid foo”,反之亦然.
这是我创建的简单特征及其伴随对象:
trait TextConvertible[T] { def convertTo(obj: T): String def convertFrom(text: String): T } object TextConvertible { import language.experimental.macros import QuasiTest.materializeTextConvertible_impl implicit def materializeTextConvertible[T]: TextConvertible[T] = macro materializeTextConvertible_impl[T] }
这是宏:
object QuasiTest { import reflect.macros._ def materializeTextConvertible_impl[T: c.WeakTypeTag](c: Context): c.Expr[TextConvertible[T]] = { import c.universe._ val tpe = weakTypeOf[T] val fields = tpe.declarations.collect { case field if field.isMethod && field.asMethod.isCaseAccessor => field.asMethod.accessed } val strConvertTo = fields.map { field => q"obj.$field" }.reduce[Tree] { case (acc,elem) => q"""$acc + " " + $elem""" } val strConvertFrom = fields.zipWithIndex map { case (field,index) => q"splitted($index)" } val quasi = q""" new TextConvertible[$tpe] { def convertTo(obj: $tpe) = $strConvertTo def convertFrom(text: String) = { val splitted = text.split(" ") new $tpe(..$strConvertFrom) } } """ c.Expr[TextConvertible[T]](quasi) } }
产生
{ final class $anon extends TextConvertible[User] { def <init>() = { super.<init>(); () }; def convertTo(obj: User) = obj.id.$plus(" ").$plus(obj.name); def convertFrom(text: String) = { val splitted = text.split(" "); new User(splitted(0),splitted(1)) } }; new $anon() }
生成的代码看起来很好,但是我在类中得到错误值id在尝试使用宏时,用户无法在编译中访问用户.
我怀疑我在字段中使用了错误的类型.我尝试了field.asMethod.accessed.name,但它导致def convertTo(obj:User)= obj.id.$plus(“”).$plus(obj.name); (注意id和name之后的额外空格),这自然导致错误值id不是User的成员.
我究竟做错了什么?
解决方法
啊,在发出问题后几乎立刻就知道了.
我换了线
val fields = tpe.declarations.collect { case field if field.isMethod && field.asMethod.isCaseAccessor => field.asMethod.accessed }
至
val fields = tpe.declarations.collect { case field if field.isMethod && field.asMethod.isCaseAccessor => field.name }
这解决了问题.