加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala Macros:使用quasiquotes访问成员

发布时间:2020-12-16 18:49:31 所属栏目:安全 来源:网络整理
导读:我正在尝试实现一个隐式的物化器,如下所述: http://docs.scala-lang.org/overviews/macros/implicits.html 我决定创建一个宏,使用quasiquotes将case类从String转换为String,以进行原型设计.例如: case class User(id: String,name: String)val foo = User(
我正在尝试实现一个隐式的物化器,如下所述: 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
}

这解决了问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读