Created
April 4, 2025 04:09
-
-
Save xuwei-k/e85d69ec58b5ebaf37557fce3a758bfb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
scalaVersion := "3.6.4" |
This file contains hidden or 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
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) | |
} | |
} | |
} | |
} |
This file contains hidden or 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
@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