Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala “编译错误”;未找到值…”;宏扩展后_Scala_Scala Macros_Scala Reflect - Fatal编程技术网

Scala “编译错误”;未找到值…”;宏扩展后

Scala “编译错误”;未找到值…”;宏扩展后,scala,scala-macros,scala-reflect,Scala,Scala Macros,Scala Reflect,我是scala宏的新手。我正在为XDB客户端编写一个自动JSON编写器/读取器 读者看起来像: trait InfluxReader[T] { def read(js: JsArray): T } Warning:scalac: { final class $anon extends InfluxReader[Test] { def <init>() = { super.<init>(); () }; def r

我是scala宏的新手。我正在为XDB客户端编写一个自动JSON编写器/读取器

读者看起来像:

trait InfluxReader[T] {
  def read(js: JsArray): T
} 
Warning:scalac: {
  final class $anon extends InfluxReader[Test] {
    def <init>() = {
      super.<init>();
      ()
    };
    def read(js: JsArray): Test = js.elements.tail match {
      case Vector(JsNumber(age), JsString(name)) => new Test(age = age, name = name)
      case _ => throw DeserializationException("Can\'t deserialize Test object")
    }
  };
  new $anon()
}
流入格式:

object InfluxFormatter {
  /**
    * Generate InfluxReader for type ${A}
    */
  def reader[A]: InfluxReader[A] = macro InfluxFormatterImpl.reader_impl[A]
}
InfluxFormatterImpl:

private[macros] object InfluxFormatterImpl {
  /***
    * Generate AST for current type at compile time.
    * @tparam T - Type parameter for whom will be generated AST
    */
  def reader_impl[T: c.WeakTypeTag](c: blackbox.Context): c.universe.Tree = {
    import c.universe._

    val tpe = c.weakTypeOf[T]

    val methods = tpe.decls.toList collect {
      case m: MethodSymbol if m.isCaseAccessor =>
        m.name.decodedName.toString -> m.returnType.dealias
    }

    if (methods.lengthCompare(1) < 0) {
      c.abort(c.enclosingPosition, "Type parameter must be a case class with more then 1 fields")
    }

    val bool = typeOf[Boolean].dealias
    val int = typeOf[Int].dealias
    val long = typeOf[Long].dealias
    val double = typeOf[Double].dealias
    val string = typeOf[String].dealias

    val params = methods
      .map(_._1)
      .sorted
      .map(v => TermName(v))
      .map(v => q"$v = $v")

    val jsParams = methods
      .sortBy(_._1) // influx return results in alphabetical order
      .map { case (k, v) => TermName(k) -> v }
      .map {
        case (name, `bool`) => q"JsBoolean($name)"
        case (name, `string`) => q"JsString($name)"
        case (name, `int`) => q"JsNumber($name)"
        case (name, `long`) => q"JsNumber($name)"
        case (name, `double`) => q"JsNumber($name)"
        case (_, other) => c.abort(c.enclosingPosition, s"Unknown type $other")
      }

    val failureMsg = s"Can't deserialize $tpe object"

    val result = q"""
        new InfluxReader[$tpe] {
          def read(js: JsArray): $tpe = js.elements.tail match {
            case Vector(..$jsParams) => new $tpe(..$params)
            case _ => throw DeserializationException($failureMsg)
          }
       }"""

    result
  }
}
编译
MacroReaderSpec
失败,出现编译错误:

Error:(14, 36) not found: value age
val rd = InfluxFormatter.reader[Test]
Error:(14, 36) not found: value name
val rd = InfluxFormatter.reader[Test]
使用编译器选项
“-Ymacro-debug-lite”
,它看起来像:

trait InfluxReader[T] {
  def read(js: JsArray): T
} 
Warning:scalac: {
  final class $anon extends InfluxReader[Test] {
    def <init>() = {
      super.<init>();
      ()
    };
    def read(js: JsArray): Test = js.elements.tail match {
      case Vector(JsNumber(age), JsString(name)) => new Test(age = age, name = name)
      case _ => throw DeserializationException("Can\'t deserialize Test object")
    }
  };
  new $anon()
}
为什么它找不到它?我认为它不能将其识别为模式匹配的表达式,也不能调用提取器。如果是,如何更改?也许它在某种程度上称之为“得分不足”? 我使用的是scala 2.12.4。SBT1.1.0。 可以找到源代码


谢谢大家。

它通过使用
pq
quasikotes重写代码得到修复。此外,所有标识符必须包装到
术语名(41;
。可以找到一个工作示例