Skip to content

Commit 06e61a9

Browse files
committed
Implement full example with Mirrors of Sums and Products
1 parent a69c3ca commit 06e61a9

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

tests/run-macros/i8007.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ true
77

88
false
99

10+
true
11+
12+
true
13+
14+
false
15+

tests/run-macros/i8007/Macro_3.scala

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@ object Eq {
2727

2828
def summonAll[T](t: Type[T])(given qctx: QuoteContext): List[Expr[Eq[_]]] = t match {
2929
case '[$tpe *: $tpes] =>
30-
println(tpe.show)
31-
summonExpr(given '[Eq[$tpe]]).get :: summonAll(tpes)
30+
val eqInstance = summonExpr(given '[Eq[$tpe]]) match {
31+
case Some(ev) => ev
32+
case None => derived(given tpe, qctx)
33+
}
34+
eqInstance :: summonAll(tpes)
3235
case '[Unit] => Nil
3336
}
3437

35-
def derived[T: Type](ev: Expr[Mirror.Of[T]])(given qctx: QuoteContext): Expr[Eq[T]] = {
38+
given derived[T: Type](given qctx: QuoteContext): Expr[Eq[T]] = {
3639
import qctx.tasty.{_, given}
3740

41+
val ev: Expr[Mirror.Of[T]] = summonExpr(given '[Mirror.Of[T]]).get
42+
3843
ev match {
3944
case '{ $m: Mirror.ProductOf[T] { type MirroredElemTypes = $elementTypes }} =>
4045
val elemInstances = summonAll(elementTypes)
@@ -71,20 +76,7 @@ object Eq {
7176
}
7277

7378
object Macro3 {
79+
inline def [T](x: =>T) === (y: =>T)(given eq: Eq[T]): Boolean = eq.eqv(x, y)
7480

75-
76-
77-
inline def test3[T](value: =>T, value2: =>T): Boolean = ${ test3Impl('value, 'value2) }
78-
79-
def test3Impl[T: Type](value: Expr[T], value2: Expr[T])(given qctx: QuoteContext): Expr[Boolean] = {
80-
import qctx.tasty.{_, given}
81-
82-
val mirrorTpe = '[Mirror.Of[T]]
83-
val mirrorExpr = summonExpr(given mirrorTpe).get
84-
val derivedInstance = Eq.derived(mirrorExpr)
85-
86-
'{
87-
$derivedInstance.eqv($value, $value2)
88-
}
89-
}
81+
implicit inline def eqGen[T]: Eq[T] = ${ Eq.derived[T] }
9082
}

tests/run-macros/i8007/Test_4.scala

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import Macro1._
22
import Macro2._
33
import Macro3._
4-
import scala.deriving._
4+
import Macro3.eqGen
55

66
case class Person(name: String, age: Int)
77

8-
enum Opt[+T] derives Eq {
8+
enum Opt[+T] {
99
case Sm(t: T)
1010
case Nn
1111
}
1212

1313
@main def Test() = {
1414
import Opt._
15+
import Eq.{given, _}
1516

1617
val t1 = test1(Person("Test", 23))
1718
println(t1)
@@ -21,15 +22,23 @@ enum Opt[+T] derives Eq {
2122
println(t2)
2223
println
2324

24-
val t3 = test3(Person("Test", 23), Person("Test", 23))
25+
val t3 = Person("Test", 23) === Person("Test", 23)
2526
println(t3) // true
2627
println
2728

28-
val t4 = test3(Person("Test", 23), Person("Test", 24))
29+
val t4 = Person("Test", 23) === Person("Test", 24)
2930
println(t4) // false
3031
println
3132

32-
val t5 = test3(Sm(Person("Test", 23)), Sm(Person("Test", 23)))
33+
val t5 = Sm(23) === Sm(23)
3334
println(t5) // true
3435
println
36+
37+
val t6 = Sm(Person("Test", 23)) === Sm(Person("Test", 23))
38+
println(t6) // true
39+
println
40+
41+
val t7 = Sm(Person("Test", 23)) === Sm(Person("Test", 24))
42+
println(t7) // false
43+
println
3544
}

0 commit comments

Comments
 (0)