0
trait Show[T] {
  def show(t: T): String
}

给这样Show的类型类,我想为案例类生成展示,比如

 def caseClassShow[A](using Type[A], Quotes): Expr[Show[A]] = {
    import quotes.reflect._
    def shows(caseClassExpr: Expr[A]): Expr[String] = { 
      val caseClassTerm = caseClassExpr.asTerm
      val parts = TypeRepr.of[A].typeSymbol.caseFields.collect {
        case cf if cf.isValDef =>
          val valDefTree = cf.tree.asInstanceOf[ValDef]
          val valType = valDefTree.tpt
          val showCtor = TypeTree.of[Show[_]]
          val valShowType = Applied(showCtor, List(valType))
          val showInstance = Expr.summon[valShowType] // compile error, how to summon the instance here
          val valuePart = Apply(Select.unique(showInstance, "show"), List(Select(caseClassTerm, cf)))
          '{s"${Expr(cf.name)}:${valuePart}"}
      }
      val strParts = Expr.ofList(parts)
      '{$strParts.mkString(",")}
    }
    '{
      new Show[A] {
        def show(a: A) = {
          ${shows('{a})}
        }
      }
    }
  }

但是该showInstance部分不会编译,那么如何在Show[X]这里调用一个隐式?

4

1 回答 1

0

Implicits.search 如果没有类型 arg 可用于调用隐式实例Expr.summon

  val valDefTree = cf.tree.asInstanceOf[ValDef]
  val valType = valDefTree.tpt
  val showCtor = TypeRepr.typeConstructorOf(classOf[Show[_]])
  val valShowType = showCtor.appliedTo(valType.tpe)
  Implicits.search(valShowType) match {
    case si: ImplicitSearchSuccess =>
      val siExpr: Expr[Show[Any]] = si.tree.asExpr.asInstanceOf[Expr[Show[Any]]]
      val valueExpr = Select(caseClassTerm, cf).asExpr
      '{$siExpr.show($valueExpr)}
  }
于 2021-01-04T09:20:13.697 回答