Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created April 4, 2025 04:09
Show Gist options
  • Save xuwei-k/e85d69ec58b5ebaf37557fce3a758bfb to your computer and use it in GitHub Desktop.
Save xuwei-k/e85d69ec58b5ebaf37557fce3a758bfb to your computer and use it in GitHub Desktop.
scalaVersion := "3.6.4"
import scala.quoted.*
object Foo {
inline def f[A](inline x: A): List[(String, String)] = ${ impl('x) }
def impl[A](using q: Quotes)(x: Expr[A]): Expr[List[(String, String)]] = {
import q.reflect.*
x.asTerm match {
case Inlined(_, _, Block(_, Match(_, i :: Nil))) =>
i match {
case CaseDef(TypedOrTest(t: Unapply, _), _ , _) =>
val values = t.patterns.map {
case Bind(a, TypedOrTest(_,b)) =>
(a, b.tpe.show)
}
Expr(values)
}
}
}
}
@main def main(): Unit = {
val x: Any = 2
val result = Foo.f{
x match {
case (foo: Int, bar: String) => 3
}
}
println(result)
assert(result == List("foo" -> "scala.Int", "bar" -> "scala.Predef.String"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment