snippets/std/macro.scala
2024-11-02 20:34:57 +08:00

21 lines
455 B
Scala

//> using scala 3.4.0
import scala.quoted.*
inline def printTree[A](inline x: A) = ${printTreeImpl[A]('x)}
private def printTreeImpl[A](x: Expr[A])(using Quotes) = {
import quotes.reflect.*
def print0(t: Term): Unit = {
t match {
case Inlined(_, _, n) =>
print0(n)
case Lambda(vals, body) =>
println(s"Lambda body ${body}")
case o =>
println(s"Lambda body ${o}")
}
}
print0(x.asTerm)
'{()}
}