@@ -44,11 +44,11 @@ from the signature. The body of the `derived` method is shown below:
44
44
given derived [T : Type ](using qctx : QuoteContext ) as Expr [Eq [T ]] = {
45
45
import qctx .reflect ._
46
46
47
- val ev : Expr [Mirror .Of [T ]] = Expr .summon( using ' [Mirror .Of [T ]]) .get
47
+ val ev : Expr [Mirror .Of [T ]] = Expr .summon[Mirror .Of [T ]].get
48
48
49
49
ev match {
50
- case ' { $m : Mirror .ProductOf [T ] { type MirroredElemTypes = $ elementTypes }} =>
51
- val elemInstances = summonAll( elementTypes)
50
+ case ' { $m : Mirror .ProductOf [T ] { type MirroredElemTypes = elementTypes }} =>
51
+ val elemInstances = summonAll[ elementTypes]
52
52
val eqProductBody : (Expr [T ], Expr [T ]) => Expr [Boolean ] = (x, y) => {
53
53
elemInstances.zipWithIndex.foldLeft(Expr (true : Boolean )) {
54
54
case (acc, (elem, index)) =>
@@ -84,17 +84,17 @@ Instead we extract the tuple-type for element types using pattern matching over
84
84
quotes and more specifically of the refined type:
85
85
86
86
``` scala
87
- case ' { $m : Mirror .ProductOf [T ] { type MirroredElemTypes = $ elementTypes } } => ...
87
+ case ' { $m : Mirror .ProductOf [T ] { type MirroredElemTypes = elementTypes }} => ...
88
88
```
89
89
90
90
The implementation of ` summonAll ` as a macro can be show below assuming that we
91
91
have the given instances for our primitive types:
92
92
93
93
``` scala
94
- def summonAll [T ]( t : Type [ T ]) (using qctx : QuoteContext ): List [Expr [Eq [_]]] = t match {
95
- case ' [String *: $ tpes] => ' { summon[Eq [String ]] } :: summonAll( tpes)
96
- case ' [Int *: $ tpes] => ' { summon[Eq [Int ]] } :: summonAll( tpes)
97
- case ' [$ tpe *: $ tpes] => derived( using tpe, qctx) :: summonAll( tpes)
94
+ def summonAll [T : Type ] (using qctx : QuoteContext ): List [Expr [Eq [_]]] = t match {
95
+ case ' [String *: tpes] => ' { summon[Eq [String ]] } :: summonAll[ tpes]
96
+ case ' [Int *: tpes] => ' { summon[Eq [Int ]] } :: summonAll[ tpes]
97
+ case ' [tpe *: tpes] => derived[ tpe] :: summonAll[ tpes]
98
98
case ' [EmptyTuple ] => Nil
99
99
}
100
100
```
@@ -120,8 +120,8 @@ Alternatively and what is shown below is that we can call the `eqv` method
120
120
directly. The ` eqGen ` can trigger the derivation.
121
121
122
122
``` scala
123
- extension [T ](x : => T )
124
- inline def === (y : => T )(using eq : Eq [T ]): Boolean = eq.eqv(x, y)
123
+ extension [T ](inline x : T )
124
+ inline def === (inline y : T )(using eq : Eq [T ]): Boolean = eq.eqv(x, y)
125
125
126
126
implicit inline def eqGen [T ]: Eq [T ] = $ { Eq .derived[T ] }
127
127
```
@@ -144,7 +144,7 @@ The full code is shown below:
144
144
``` scala
145
145
import scala .deriving ._
146
146
import scala .quoted ._
147
- import scala . quoted . matching . _
147
+
148
148
149
149
trait Eq [T ] {
150
150
def eqv (x : T , y : T ): Boolean
@@ -169,21 +169,21 @@ object Eq {
169
169
def eqv (x : T , y : T ): Boolean = body(x, y)
170
170
}
171
171
172
- def summonAll [T ]( t : Type [ T ]) (using qctx : QuoteContext ): List [Expr [Eq [_]]] = t match {
173
- case ' [String *: $ tpes] => ' { summon[Eq [String ]] } :: summonAll( tpes)
174
- case ' [Int *: $ tpes] => ' { summon[Eq [Int ]] } :: summonAll( tpes)
175
- case ' [$ tpe *: $ tpes] => derived( using tpe, qctx) :: summonAll( tpes)
172
+ def summonAll [T : Type ] (using qctx : QuoteContext ): List [Expr [Eq [_]]] = t match {
173
+ case ' [String *: tpes] => ' { summon[Eq [String ]] } :: summonAll[ tpes]
174
+ case ' [Int *: tpes] => ' { summon[Eq [Int ]] } :: summonAll[ tpes]
175
+ case ' [tpe *: tpes] => derived[ tpe] :: summonAll[ tpes]
176
176
case ' [EmptyTuple ] => Nil
177
177
}
178
178
179
179
given derived [T : Type ](using qctx : QuoteContext ) as Expr [Eq [T ]] = {
180
180
import qctx .reflect ._
181
181
182
- val ev : Expr [Mirror .Of [T ]] = Expr .summon( using ' [Mirror .Of [T ]]) .get
182
+ val ev : Expr [Mirror .Of [T ]] = Expr .summon[Mirror .Of [T ]].get
183
183
184
184
ev match {
185
- case ' { $m : Mirror .ProductOf [T ] { type MirroredElemTypes = $ elementTypes }} =>
186
- val elemInstances = summonAll( elementTypes)
185
+ case ' { $m : Mirror .ProductOf [T ] { type MirroredElemTypes = elementTypes }} =>
186
+ val elemInstances = summonAll[ elementTypes]
187
187
val eqProductBody : (Expr [T ], Expr [T ]) => Expr [Boolean ] = (x, y) => {
188
188
elemInstances.zipWithIndex.foldLeft(Expr (true : Boolean )) {
189
189
case (acc, (elem, index)) =>
@@ -197,8 +197,8 @@ object Eq {
197
197
eqProduct((x : T , y : T ) => $ {eqProductBody(' x , ' y )})
198
198
}
199
199
200
- case ' { $m : Mirror .SumOf [T ] { type MirroredElemTypes = $ elementTypes }} =>
201
- val elemInstances = summonAll( elementTypes)
200
+ case ' { $m : Mirror .SumOf [T ] { type MirroredElemTypes = elementTypes }} =>
201
+ val elemInstances = summonAll[ elementTypes]
202
202
val eqSumBody : (Expr [T ], Expr [T ]) => Expr [Boolean ] = (x, y) => {
203
203
val ordx = ' { $m.ordinal($x) }
204
204
val ordy = ' { $m.ordinal($y) }
@@ -217,8 +217,8 @@ object Eq {
217
217
}
218
218
219
219
object Macro3 {
220
- extension [T ](x : => T )
221
- inline def === (y : => T )(using eq : Eq [T ]): Boolean = eq.eqv(x, y)
220
+ extension [T ](inline x : T )
221
+ inline def === (inline y : T )(using eq : Eq [T ]): Boolean = eq.eqv(x, y)
222
222
223
223
implicit inline def eqGen [T ]: Eq [T ] = $ { Eq .derived[T ] }
224
224
}
0 commit comments