Skip to content

Commit d8fcbbf

Browse files
committed
WIP
1 parent 315071c commit d8fcbbf

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

tests/run-macros/i8007/Macro_1.scala

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import scala.quoted._
33
import scala.quoted.matching._
44
import scala.compiletime.{erasedValue, summonFrom, constValue}
55

6-
object Macro {
6+
object Macro1 {
77
case class Person(name: String, age: Int)
88

99
def mirrorFields[T](t: Type[T])(given qctx: QuoteContext): List[String] =
@@ -12,23 +12,21 @@ object Macro {
1212
case '[Unit] => Nil
1313
}
1414

15-
inline def usingSummonFrom[T](value: =>T): List[String] =
16-
${ usingSummonFromImpl('value) }
15+
// Macro method 1
16+
// Demonstrates the use of quoted pattern matching
17+
// over a refined type, extracting the tuple type
18+
// for e.g., MirroredElemLabels
19+
inline def test1[T](value: =>T): List[String] =
20+
${ test1Impl('value) }
1721

18-
def usingSummonFromImpl[T: Type](value: Expr[T])(given qctx: QuoteContext): Expr[List[String]] = {
22+
def test1Impl[T: Type](value: Expr[T])(given qctx: QuoteContext): Expr[List[String]] = {
1923
import qctx.tasty.{_, given}
2024

21-
2225
val mirrorTpe = '[Mirror.Of[T]]
2326

2427
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]))
28+
case '{ $m: Mirror.ProductOf[T]{ type MirroredElemLabels = $t } } => {
29+
Expr(mirrorFields(t))
3230
}
3331
}
3432
}

tests/run-macros/i8007/Macro_2.scala

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import scala.deriving._
2+
import scala.quoted._
3+
import scala.quoted.matching._
4+
import scala.compiletime.{erasedValue, summonFrom, constValue}
5+
6+
object Macro2 {
7+
trait JsonEncoder[T] {
8+
def encode(elem: T): String
9+
}
10+
11+
object JsonEncoder {
12+
def derived[T: Type](ev: Expr[Mirror.Of[T]])(given qctx: QuoteContext): Expr[JsonEncoder[T]] = '{
13+
new JsonEncoder[T] {
14+
def encode(elem: T): String = $ev match {
15+
case '{ $m: Mirror.ProductOf[T] /*{ type MirroredElemLabels = $t } */ } => {
16+
17+
// val elems = mirrorFields(t)
18+
// "{" + (elems).mkString(", ") + "}"
19+
20+
"test"
21+
}
22+
case '{ $m: Mirror.SumOf[T] } => "not supporting this case yet"
23+
}
24+
}
25+
}
26+
}
27+
28+
inline def test2[T](value: =>T): Unit =
29+
${ test2Impl('value) }
30+
31+
def test2Impl[T: Type](value: Expr[T])(given qctx: QuoteContext): Expr[Unit] = {
32+
import qctx.tasty.{_, given}
33+
34+
val mirrorTpe = '[Mirror.Of[T]]
35+
val mirror = summonExpr(given mirrorTpe).get
36+
val res = JsonEncoder.derived(mirror)
37+
38+
'{
39+
given JsonEncoder[T] = $res
40+
()
41+
}
42+
}
43+
}

tests/run-macros/i8007/Test_2.scala

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/run-macros/i8007/Test_3.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Macro1._
2+
import Macro2._
3+
4+
@main def Test() = {
5+
val list = test1[Person](Person("Test", 23))
6+
println(list)
7+
8+
test2[Person](Person("Test", 23))
9+
}

0 commit comments

Comments
 (0)