使用准引用访问成员

使用准引用访问成员

本文介绍了Scala宏:使用准引用访问成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试实现此处所述的隐式实现器: http://docs.scala- lang.org/overviews/macros/implicits.html

I'm trying to implement an implicit materializer as described here: http://docs.scala-lang.org/overviews/macros/implicits.html

我决定创建一个宏,该宏使用准引用将案例类与String之间进行转换,以进行原型制作.例如:

I decided to create a macro that converts a case class from and to a String using quasiquotes for prototyping purposes. For example:

case class User(id: String, name: String)
val foo = User("testid", "foo")

foo转换为文本应导致"testid foo",反之亦然.

Converting foo to text should result in "testid foo" and vice versa.

这是我创建的简单特征及其伴侣对象:

Here is the simple trait and its companion object I have created:

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.

The generated code looks fine, but yet I get the error value id in class User cannot be accessed in User in compilation while trying to use the macro.

我怀疑我对字段使用了错误的类型.我尝试了field.asMethod.accessed.name,但结果为def convertTo(obj: User) = obj.id .$plus(" ").$plus(obj.name );(请注意idname之后的多余空格),这自然会导致错误value id is not a member of User.

I suspect I am using a wrong type for fields. I tried field.asMethod.accessed.name, but it results in def convertTo(obj: User) = obj.id .$plus(" ").$plus(obj.name ); (note the extra spaces after id and name), which naturally results in the error value id is not a member of User.

我在做什么错了?

推荐答案

啊,在发完我的问题后几乎立刻就弄清了.

Ah, figured it out almost immediately after sending my question.

我改变了台词

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
}

解决了这个问题.

这篇关于Scala宏:使用准引用访问成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:06