Created
February 28, 2021 11:21
-
-
Save ajrnz/0ce8024b96381cdab391e703dd4d1e0b to your computer and use it in GitHub Desktop.
Macro Testing (macro)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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