forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi12253.scala
31 lines (23 loc) · 1.06 KB
/
i12253.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import scala.quoted.{given, *}
import deriving.*, compiletime.*
object MacroUtils:
transparent inline def extractNameFromSelector[To, T](inline code: To => T) = ${extractNameFromSelectorImpl('code)}
def extractNameFromSelectorImpl[To: Type, T: Type](code: Expr[To => T])(using q1: Quotes): Expr[String] =
import quotes.reflect.*
val extractors = new Extractors
code.asTerm match
case extractors.InlinedLambda(_, Select(_, name)) => Expr(name) // warn // warn
case t => report.throwError(s"Illegal argument to extractor: ${code.show}, in tasty: $t")
class Extractors(using val q2: Quotes):
//attempt to strip away consecutive inlines in AST and extract only final lambda
import quotes.reflect.*
object InlinedLambda:
def unapply(arg: Term): Option[(List[ValDef], Term)] =
arg match
case Inlined(_, _, Lambda(vals, term)) => Some((vals, term))
case Inlined(_, _, nested) => InlinedLambda.unapply(nested)
case t => None
end InlinedLambda
end Extractors
end MacroUtils
// nopos-warn deprecation