diff --git a/tests/pos-macros/i12253/Macro_1.scala b/tests/pos-macros/i12253/Macro_1.scala new file mode 100644 index 000000000000..43eb73542c33 --- /dev/null +++ b/tests/pos-macros/i12253/Macro_1.scala @@ -0,0 +1,24 @@ + +import scala.quoted.* +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 Quotes): Expr[String] = + import quotes.reflect.* + code.asTerm match + case InlinedLambda(_, Select(_, name)) => Expr(name) + case t => Expr("") + + object InlinedLambda: + def unapply(using Quotes)(arg: quotes.reflect.Term): Option[(List[quotes.reflect.ValDef], quotes.reflect.Term)] = + import quotes.reflect.* + arg match + case Inlined(_, _, Lambda(vals, term)) => Some((vals, term)) + case Inlined(_, _, nested) => InlinedLambda.unapply(nested) + case t => None + end InlinedLambda + +end MacroUtils + diff --git a/tests/pos-macros/i12253/Test_2.scala b/tests/pos-macros/i12253/Test_2.scala new file mode 100644 index 000000000000..61aae41d1ee0 --- /dev/null +++ b/tests/pos-macros/i12253/Test_2.scala @@ -0,0 +1,4 @@ +object Usage: + case class Bar(x: Int, y: String, z: (Double, Double)) + MacroUtils.extractNameFromSelector[Bar, String](_.y + "abc") +end Usage