【问题标题】:How to use Shapeless in a Quasiquote?如何在 Quasiquote 中使用 Shapeless?
【发布时间】:2015-10-19 04:06:08
【问题描述】:

我正在尝试使用 Scalaquasiquote 内部调用 Shapeless 宏,但我没有得到想要的结果。

我的宏没有返回任何错误,但它没有将 Witness(fieldName) 扩展为 Witness.Lt[String]

val implicits = schema.fields.map { field =>
  val fieldName:String = field.name
  val fieldType = TypeName(field.valueType.fullName)
  val in = TermName("implicitField"+fieldName)
  val tn = TermName(fieldName)
  val cc = TermName("cc")
  q"""implicit val $in = Field.apply[$className,$fieldType](Witness($fieldName), ($cc:   $className) => $cc.$tn)"""
}

这是我的Field 定义:

sealed abstract class Field[CC, FieldName] {
  val  fieldName: String
  type fieldType

  // How to extract this field
  def  get(cc : CC) : fieldType
}

object Field {
  // fieldType is existencial in Field but parametric in Fied.Aux
  // used to explict constraints on fieldType
  type Aux[CC, FieldName, fieldType_] = Field[CC, FieldName] {
    type fieldType = fieldType_
  }

  def apply[CC, fieldType_](fieldWitness : Witness.Lt[String], ext : CC => fieldType_) : Field.Aux[CC, fieldWitness.T, fieldType_] =
    new Field[CC, fieldWitness.T] {
      val fieldName  : String = fieldWitness.value
      type fieldType = fieldType_
      def get(cc : CC) : fieldType = ext(cc)
    }
}

在这种情况下,我生成的隐式如下所示:

implicit val implicitFieldname : Field[MyCaseClass, fieldWitness.`type`#T]{
  override type fieldType = java.lang.String
}

如果它是在 quasiquote 之外定义的,它会生成如下内容:

implicit val implicitFieldname : Field.Aux[MyCaseClass, Witness.Lt[String]#T, String] = ...

有什么可以做的吗?

【问题讨论】:

  • 你在宏注释中使用这个吗?您是否尝试过为$in 提供类型注释(我认为需要使用ConstantType)?
  • @TravisBrown 是的,我正在使用宏注释(Macro Paradise)构建它。我试图提供这样的类型:q"""implicit val $in : Field.Aux[$className, Witness.Lt[String]#T, String] = Field.apply[$className,$fieldType](Witness($fieldName), ($cc: $className) => $cc.$tn)"""
  • 不过,您需要在类型注释中使用特定的字段名称(例如,请参阅我的旧的预Shapeless 2.0 博客文章here 以获取使用ConstantType 的示例)。你碰巧有一个完整的工作示例吗?

标签: scala scala-macros shapeless scala-quasiquotes


【解决方案1】:

这是我使用旧式宏注释的工作解决方案。

import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
import scala.annotation.StaticAnnotation

class fieldable extends StaticAnnotation {
  def macroTransform(annottees: Any*): Any = macro fieldableMacro.impl
}

object fieldableMacro {
  def impl(c: Context)(annottees: c.Expr[Any]*): c.Tree = {
    import c.universe._
    annottees.map(_.tree) match {
      case (param @ q"case class $className(..$fields)") :: Nil => {
        val implicits = fields.collect {
          case field @ q"$mods val $tname: $tpt" => q"""
            implicit val $tname = Field.apply[$className,$tpt](
              Witness(${tname.decodedName.toString}), _.$tname
            )"""
        }; q"$param; object ${className.toTermName} {..$implicits}"
      }
    }
  }
}

当然,可以使用更好的准引号对其进行改进,但我的目标是显示尽可能清晰的内容。

它可以用作:

@fieldable
case class MyCaseClass(foo: String, bar: Int)

这会产生一个 MyCaseClass 伴随对象,其中包含所需的 Fields 隐含:

implicit val foo = Field.apply[MyCaseClass, String](Witness("foo"), ((x$1) => x$1.foo));
implicit val bar = Field.apply[MyCaseClass, Int](Witness("bar"), ((x$2) => x$2.bar));

正如已经指出的那样,如果没有完整的工作示例,很难写出详尽的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多