【问题标题】:Lifting string with ScalametaScalameta 起重绳
【发布时间】:2018-06-07 10:05:01
【问题描述】:

我想注入一个方法“toCSV”来将一个类解析为 CSV 字符串。我的函数有两个参数:

  • Seq[String] : 标头
  • Seq[Seq[Any]]:字段

我的宏:

class model extends scala.annotation.StaticAnnotation {

  inline def apply(defn: Any): Any = meta {
    defn match {
      case cls @ Defn.Class(_, _, _, Ctor.Primary(_, _, paramss), template) =>

        def createToCSV(paramss: Seq[Seq[Term.Param]]): Defn.Def = {

          val names = paramss.flatten.map(param => param.name.syntax)
          val fields = paramss.map(_.map(param => Term.Name(param.name.value)))

          q"""def toCSV =
             CSVTools.toCSV(Seq(...$names), Seq(Seq(...$fields)))"""
        }

        val toCSVImpl = createToCSV(paramss)

        val templateStats: Seq[Stat] = toCSVImpl +: template.stats.getOrElse(Nil)
        cls.copy(templ = template.copy(stats = Some(templateStats)))
      case _ =>
        println(defn.structure)
        abort("@model must annotate a class.")
    }
  }
}

变量 Term 是一个 Seq[String] 并且 quasiquotes 语法只接受一个 Term。 因此,出现以下错误:

[error] /home/xxxxx/intellij/workspace/heroes-scala-meta/macros/src/main/scala/examples/model.scala:18: type mismatch when unquoting;
[error]  found   : scala.collection.immutable.Seq[String]
[error]  required: scala.collection.immutable.Seq[scala.collection.immutable.Seq[scala.meta.Term.Arg]]
[error]              CSVTools.toCSV(Seq(...$names), Seq(Seq(...$fields)))"""
[error]                                    ^
[error] one error found
[error] (macros/compile:compileIncremental) Compilation failed

你有解决办法吗?

提前致谢,

【问题讨论】:

    标签: scala scala-quasiquotes scalameta quasiquotes


    【解决方案1】:

    这可能有点 hacky,但它似乎有效:将您的 names 显式映射到字符串文字

    class model extends scala.annotation.StaticAnnotation {
    
      inline def apply(defn: Any): Any = meta {
        defn match {
          case cls@Defn.Class(_, _, _, Ctor.Primary(_, _, paramss), template) =>
    
            def createToCSV(paramss: Seq[Seq[Term.Param]]): Defn.Def = {
    
              val names: Seq[String] = paramss.flatten.map(param => param.name.syntax)
              val fields = paramss.map(_.map(param => Term.Name(param.name.value)))
    
              val nameLiterals: Seq[Lit.String] = names.map(n => q"$n".asInstanceOf[Lit.String])
    
              q"""def toCSV =
                  CSVTools.toCSV(scala.collection.immutable.Seq(..$nameLiterals),
                                 scala.collection.immutable.Seq(scala.collection.immutable.Seq(...$fields)))"""
            }
    
            val toCSVImpl = createToCSV(paramss)
    
            val templateStats: Seq[Stat] = toCSVImpl +: template.stats.getOrElse(Nil)
            cls.copy(templ = template.copy(stats = Some(templateStats)))
          case _ =>
            println(defn.structure)
            abort("@model must annotate a class.")
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2017-09-09
      • 2017-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多