Skip to content

Commit b7be482

Browse files
Merge pull request #10382 from ghostdogpr/patch-1
Fix example code from How to write a type class derived method using macros
2 parents 45023d8 + 4bda7be commit b7be482

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

docs/docs/reference/contextual/derivation-macro.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ from the signature. The body of the `derived` method is shown below:
4444
given derived[T: Type](using qctx: QuoteContext) as Expr[Eq[T]] = {
4545
import qctx.reflect._
4646

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
4848

4949
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]
5252
val eqProductBody: (Expr[T], Expr[T]) => Expr[Boolean] = (x, y) => {
5353
elemInstances.zipWithIndex.foldLeft(Expr(true: Boolean)) {
5454
case (acc, (elem, index)) =>
@@ -84,17 +84,17 @@ Instead we extract the tuple-type for element types using pattern matching over
8484
quotes and more specifically of the refined type:
8585

8686
```scala
87-
case '{ $m: Mirror.ProductOf[T] { type MirroredElemTypes = $elementTypes } } => ...
87+
case '{ $m: Mirror.ProductOf[T] { type MirroredElemTypes = elementTypes }} => ...
8888
```
8989

9090
The implementation of `summonAll` as a macro can be show below assuming that we
9191
have the given instances for our primitive types:
9292

9393
```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]
9898
case '[EmptyTuple] => Nil
9999
}
100100
```
@@ -120,8 +120,8 @@ Alternatively and what is shown below is that we can call the `eqv` method
120120
directly. The `eqGen` can trigger the derivation.
121121

122122
```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)
125125

126126
implicit inline def eqGen[T]: Eq[T] = ${ Eq.derived[T] }
127127
```
@@ -144,7 +144,7 @@ The full code is shown below:
144144
```scala
145145
import scala.deriving._
146146
import scala.quoted._
147-
import scala.quoted.matching._
147+
148148

149149
trait Eq[T] {
150150
def eqv(x: T, y: T): Boolean
@@ -169,21 +169,21 @@ object Eq {
169169
def eqv(x: T, y: T): Boolean = body(x, y)
170170
}
171171

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]
176176
case '[EmptyTuple] => Nil
177177
}
178178

179179
given derived[T: Type](using qctx: QuoteContext) as Expr[Eq[T]] = {
180180
import qctx.reflect._
181181

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
183183

184184
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]
187187
val eqProductBody: (Expr[T], Expr[T]) => Expr[Boolean] = (x, y) => {
188188
elemInstances.zipWithIndex.foldLeft(Expr(true: Boolean)) {
189189
case (acc, (elem, index)) =>
@@ -197,8 +197,8 @@ object Eq {
197197
eqProduct((x: T, y: T) => ${eqProductBody('x, 'y)})
198198
}
199199

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]
202202
val eqSumBody: (Expr[T], Expr[T]) => Expr[Boolean] = (x, y) => {
203203
val ordx = '{ $m.ordinal($x) }
204204
val ordy = '{ $m.ordinal($y) }
@@ -217,8 +217,8 @@ object Eq {
217217
}
218218

219219
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)
222222

223223
implicit inline def eqGen[T]: Eq[T] = ${ Eq.derived[T] }
224224
}

0 commit comments

Comments
 (0)