Skip to content

Commit 315071c

Browse files
committed
Update regression to use macros and summonExpr
1 parent 865ad29 commit 315071c

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

tests/run-macros/i8007.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
List("name", "age")

tests/run-macros/i8007/Macro_1.scala

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,30 @@ import scala.compiletime.{erasedValue, summonFrom, constValue}
66
object Macro {
77
case class Person(name: String, age: Int)
88

9-
// Summon a mirror for a particular type
10-
inline def summonMirror[T]: Option[Mirror.Of[T]] =
11-
summonFrom {
12-
case given m: Mirror.Of[T] => Some(m)
13-
case _ => None
9+
def mirrorFields[T](t: Type[T])(given qctx: QuoteContext): List[String] =
10+
t match {
11+
case '[$field *: $fields] => field.show :: mirrorFields(fields)
12+
case '[Unit] => Nil
1413
}
1514

16-
// Get fields from a mirror:
17-
inline def mirrorFields[Fields <: Tuple]: List[String] =
18-
inline erasedValue[Fields] match {
19-
case _: (field *: fields) => constValue[field].toString :: mirrorFields[fields]
20-
case _ => Nil
21-
}
22-
23-
inline def usingSummonFrom[T](value: =>T): String =
24-
${ usingSummonFromImpl('value, summonMirror[T]) }
15+
inline def usingSummonFrom[T](value: =>T): List[String] =
16+
${ usingSummonFromImpl('value) }
2517

26-
def usingSummonFromImpl[T: Type](value: Expr[T], m: Option[Mirror.Of[T]])(given qctx: QuoteContext): Expr[String] = {
18+
def usingSummonFromImpl[T: Type](value: Expr[T])(given qctx: QuoteContext): Expr[List[String]] = {
2719
import qctx.tasty.{_, given}
28-
val theMirror = m match { case Some(mirror) => mirror }
2920

30-
theMirror match {
31-
case m: Mirror.ProductOf[T] => println("it's a product: " + mirrorFields[m.MirroredElemLabels])
32-
}
3321

34-
'{ "Doesn't matter" }
22+
val mirrorTpe = '[Mirror.Of[T]]
23+
24+
summonExpr(given mirrorTpe).get match {
25+
case '{ $m: Mirror.ProductOf[T] } => {
26+
val typeMember = TypeSelect(m.unseal, "MirroredElemLabels")
27+
28+
type TT
29+
implicit val TT: quoted.Type[TT] = typeMember.tpe.seal.asInstanceOf[quoted.Type[TT]]
30+
31+
Expr(mirrorFields('[TT]))
32+
}
33+
}
3534
}
3635
}

0 commit comments

Comments
 (0)