我正在尝试实现一个隐式实现器,如下所述:http: //docs.scala-lang.org/overviews/macros/implicits.html
我决定创建一个宏,将 case 类从和转换为String
using quasiquotes 用于原型设计。例如:
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()
}
生成的代码看起来不错,但是value id in class User cannot be accessed in User
在尝试使用宏时出现编译错误。
我怀疑我使用了错误的字段类型。我试过field.asMethod.accessed.name
了,但它会导致(注意anddef convertTo(obj: User) = obj.id .$plus(" ").$plus(obj.name );
之后的多余空格),这自然会导致 error 。id
name
value id is not a member of User
我究竟做错了什么?