scalascala-macrosscala-3dottygeneric-derivation

Scala3 macro summon typeclass instance of a TypeTree (no type arg)


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

Give such Show typeclass, I want to generate show for case class like

 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})}
        }
      }
    }
  }

But the showInstance part won't compile, so how to summon an implicit Show[X] here ?


Solution

  • Implicits.search can be used to summon implicit instance if there is no type arg avaiable for 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)}
      }