Skip to content

Instantly share code, notes, and snippets.

@ajrnz
Created February 28, 2021 11:21
Show Gist options
  • Save ajrnz/0ce8024b96381cdab391e703dd4d1e0b to your computer and use it in GitHub Desktop.
Save ajrnz/0ce8024b96381cdab391e703dd4d1e0b to your computer and use it in GitHub Desktop.
Macro Testing (macro)
package ajr.scala3test
import scala.quoted.*
import upickle.default.Writer
def callImpl(obj: Expr[MyObject], argsExpr: Expr[Seq[Any]])(using Quotes): Expr[String] =
import quotes.reflect.report
argsExpr match
case Varargs(argExprs) =>
val argShowedExprs = argExprs.map {
case '{ $arg: tp } =>
val str = Type.show[tp]
println(s"matched arg=$arg str=$str")
Expr.summon[Writer[tp]] match
case Some(showExpr) =>
'{ upickle.default.write($arg)($showExpr) }
case None =>
report.error(s"could not find implicit for ${Type.show[Writer[tp]]}", arg); '{???}
}
val newArgsExpr = Varargs(argShowedExprs)
'{ "[" + $newArgsExpr.mkString(",") + "]" }
case _ =>
report.error(s"Args must be explicit", argsExpr)
'{???}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment